通过加或减 D 使 K 步中 N 的绝对值最小化
给定三个数N 、 D和K ,任务是在应用K 次操作后找到N的最小绝对值。在一次操作中,可以将N更改为N+D或ND 。
例子:
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.
方法:要获得最小值,请尝试将N移动到尽可能接近 0。在N处,计算N/D给出接近 0 所需的移动次数。如果K小于N/D ,则使用所有移动来接近 0 ,即使用所有移动将N更改为ND 。否则,如果(K > N/D) ,所有K移动不能在一个方向上使用,因为它超过 0,然后会增加N的值。所以首先使用N/D移动然后我们接近 0。现在K是K = K - N/D使用N/D之后。现在,来回使用这种技术,找到答案。请按照以下步骤解决此问题:
- 首先,取 N 的绝对值,因为从0到-N和N具有相同的距离。
- 初始化变量并计算totalMoves = N/D 。
- 如果K小于totalMoves ,则从N中减去D*K 。
- 否则,如果K大于totalMoves ,则从N中减去D*K ,从K中减去totalMoves 。
- 现在,检查K是否为奇数,从N中减去D。
- 否则按原样打印。
C++
// C++ program for the above approach
#include
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
2
时间复杂度: O(1)
辅助空间: O(1)