First you have to find what is the minimum number of steps to get the minimum answer and that will be simply int(x/d). If we have to perform more than int(x/d) steps then we will go forward one time and come backward in next step and so on, to retain the minimum value. If the excess steps are even then our answer remains the same but if it is odd then we subtract ādā from the answer.
Python solution:
x, k, d = map(int, input().split())
x = abs(x)
t = x/d
if t >= k:
print(abs(x - (k*d)))
elif (k-int(t))%2==0:
print(abs(x-abs(int(t)*d)))
else:
print(abs(x-abs(int(t)*d)-d))
1 Like
thanks, bro nice explanation