Skip to content
Related Articles

Related Articles

Minimize absolute value of N in K moves by adding or subtracting D

Improve Article
Save Article
Like Article
  • Difficulty Level : Expert
  • Last Updated : 25 Jan, 2022

Given three numbers N, D and K, the task is to find the minimum absolute value of N after applying K operations. In one operation, N can be changed to N+D or N-D

Example:

Input: N = 6, K = 2, D = 4
Output: 2
Explanation:
Move 1: 6 to ( 6  – 4 ) = 2
Move 2: 2 to ( 2 – 4 ) = -2
After K moves absolute value of N is 2.

Input: N = 7, K = 4, D = 3
Output: 1
Explanation:
Move 1: 7 to ( 7  – 3 ) = 4
Move 2: 4 to ( 4 + 3 ) = 7
Move 3: 7 to ( 7 – 3 ) = 4
Move 4: 4 to ( 4 – 3 ) = 1
After K moves absolute value of coordinate is 1.

 

Approach: To get the minimum value, try to move N as closer to 0 as possible. At N, calculate N/D which gives how many moves are needed to get closer to 0. If K is smaller than N/D, use all moves to get close to 0 i.e. use all moves to change N to N-D. Else if (K > N/D), all K moves can not be utilised in one direction because it exceeds 0 and then will increase the value of N. So first use N/D moves then we are close to 0. Now K is K =  K – N/D after using N/D. Now, use this technique back and forth, to find the answer. Follow the below steps to solve this problem: 

  1. First, take abs of N because from 0 to -N and N have the same distance.
  2. Initialize variable and calculate totalMoves = N/D.
  3. If K is smaller than totalMoves, subtract D*K from N.
  4. Else If K is greater than totalMoves, subtract D*K from N and totalMoves from K.
  5. Now, check if K is odd to subtract D from N.
  6. Else print as it is.

C++




// C++ program for the above approach
 
#include <iostream>
using namespace std;
 
// Function to find the minimum absolute value
// of N after applying K operations
int minAbsValue(int N, int K, int D)
{
 
    // Take totalMoves we need
    // from N to (closer to 0)
    int totalMoves = N / D;
 
    // totalMoves is greater than
    // K use all the moves
    if (totalMoves >= K) {
        N = N - K * D;
    }
    else {
 
        // Takes all moves which
        // goes closer to 0.
        N = N - totalMoves * D;
 
        // Subtract total moves we use
        K = K - totalMoves;
 
        // Now, check K, If K is odd
        // we take only one move
        if (K & 1) {
            N = abs(N - D);
        }
    }
 
    return N;
}
 
// Driver Code
int main()
{
 
    int N = 6, K = 2, D = 4;
    int ans = minAbsValue(N, K, D);
    cout << ans;
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the minimum absolute value
  // of N after applying K operations
  static int minAbsValue(int N, int K, int D)
  {
 
    // Take totalMoves we need
    // from N to (closer to 0)
    int totalMoves = N / D;
 
    // totalMoves is greater than
    // K use all the moves
    if (totalMoves >= K) {
      N = N - K * D;
    }
    else {
 
      // Takes all moves which
      // goes closer to 0.
      N = N - totalMoves * D;
 
      // Subtract total moves we use
      K = K - totalMoves;
      K = K - totalMoves;
 
      // Now, check K, If K is odd
      // we take only one move
      if (K % 2 == 1) {
        N = Math.abs(N - D);
      }
    }
 
    return N;
  }
  public static void main (String[] args) {
    int N = 6, K = 2, D = 4;
    int ans = minAbsValue(N, K, D);
    System.out.print(ans);
  }
}
 
// This code is contributed by hrithikgarg03188

Python3




# python3 program for the above approach
 
# Function to find the minimum absolute value
# of N after applying K operations
def minAbsValue(N, K, D):
 
    # Take totalMoves we need
    # from N to (closer to 0)
    totalMoves = N // D
 
    # totalMoves is greater than
    # K use all the moves
    if (totalMoves >= K):
        N = N - K * D
 
    else:
 
        # Takes all moves which
        # goes closer to 0.
        N = N - totalMoves * D
 
        # Subtract total moves we use
        K = K - totalMoves
 
        # Now, check K, If K is odd
        # we take only one move
        if (K & 1):
            N = abs(N - D)
 
    return N
 
# Driver Code
if __name__ == "__main__":
 
    N, K, D = 6, 2, 4
    ans = minAbsValue(N, K, D)
    print(ans)
 
# This code is contributed by rakeshsahni

C#




// C# program for the above approach
using System;
class GFG
{
  // Function to find the minimum absolute value
  // of N after applying K operations
  static int minAbsValue(int N, int K, int D)
  {
 
    // Take totalMoves we need
    // from N to (closer to 0)
    int totalMoves = N / D;
 
    // totalMoves is greater than
    // K use all the moves
    if (totalMoves >= K) {
      N = N - K * D;
    }
    else {
 
      // Takes all moves which
      // goes closer to 0.
      N = N - totalMoves * D;
 
      // Subtract total moves we use
      K = K - totalMoves;
      K = K - totalMoves;
 
      // Now, check K, If K is odd
      // we take only one move
      if (K % 2 == 1) {
        N = Math.Abs(N - D);
      }
    }
 
    return N;
  }
  public static void Main () {
    int N = 6, K = 2, D = 4;
    int ans = minAbsValue(N, K, D);
    Console.Write(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript




<script>
      // JavaScript code for the above approach
 
      // Function to find the minimum absolute value
      // of N after applying K operations
      function minAbsValue(N, K, D) {
 
          // Take totalMoves we need
          // from N to (closer to 0)
          let totalMoves = Math.floor(N / D);
 
          // totalMoves is greater than
          // K use all the moves
          if (totalMoves >= K) {
              N = N - K * D;
          }
          else {
 
              // Takes all moves which
              // goes closer to 0.
              N = N - totalMoves * D;
 
              // Subtract total moves we use
              K = K - totalMoves;
 
              // Now, check K, If K is odd
              // we take only one move
              if (K & 1) {
                  N = Math.abs(N - D);
              }
          }
 
          return N;
      }
 
      // Driver Code
      let N = 6, K = 2, D = 4;
      let ans = minAbsValue(N, K, D);
      document.write(ans);
       
     // This code is contributed by Potta Lokesh
  </script>

 
 

Output
2

 

Time Complexity: O(1)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!