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:
- First, take abs of N because from 0 to -N and N have the same distance.
- Initialize variable and calculate totalMoves = N/D.
- If K is smaller than totalMoves, subtract D*K from N.
- Else If K is greater than totalMoves, subtract D*K from N and totalMoves from K.
- Now, check if K is odd to subtract D from N.
- Else print as it is.
C++
#include <iostream>
using namespace std;
int minAbsValue( int N, int K, int D)
{
int totalMoves = N / D;
if (totalMoves >= K) {
N = N - K * D;
}
else {
N = N - totalMoves * D;
K = K - totalMoves;
if (K & 1) {
N = abs (N - D);
}
}
return N;
}
int main()
{
int N = 6, K = 2, D = 4;
int ans = minAbsValue(N, K, D);
cout << ans;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int minAbsValue( int N, int K, int D)
{
int totalMoves = N / D;
if (totalMoves >= K) {
N = N - K * D;
}
else {
N = N - totalMoves * D;
K = K - totalMoves;
K = K - totalMoves;
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);
}
}
|
Python3
def minAbsValue(N, K, D):
totalMoves = N / / D
if (totalMoves > = K):
N = N - K * D
else :
N = N - totalMoves * D
K = K - totalMoves
if (K & 1 ):
N = abs (N - D)
return N
if __name__ = = "__main__" :
N, K, D = 6 , 2 , 4
ans = minAbsValue(N, K, D)
print (ans)
|
C#
using System;
class GFG
{
static int minAbsValue( int N, int K, int D)
{
int totalMoves = N / D;
if (totalMoves >= K) {
N = N - K * D;
}
else {
N = N - totalMoves * D;
K = K - totalMoves;
K = K - totalMoves;
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);
}
}
|
Javascript
<script>
function minAbsValue(N, K, D) {
let totalMoves = Math.floor(N / D);
if (totalMoves >= K) {
N = N - K * D;
}
else {
N = N - totalMoves * D;
K = K - totalMoves;
if (K & 1) {
N = Math.abs(N - D);
}
}
return N;
}
let N = 6, K = 2, D = 4;
let ans = minAbsValue(N, K, D);
document.write(ans);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)