Menu Close

Minimum number of steps required to reach final destination.

You are standing at position 0 on an infinite number line.
There is a goal at position target. On each move, you can either go left or right. During the n-th move (Starting from 1), you take n steps. Return the minimum number of steps required to reach destination.
Input: target=3
Output: 2

NTar=abs(int(input('Target = ')))
k=0
while NTar>0:
    k+=1
    NTar-=k
print(NTar)
print(k if NTar%2==0 else k+1+k%2)


Input_1:
            Target = 1
Output:
             1


Input_2:
           Target = 3
Output:
             2


Input_3:
          Target = 2
Output:
            3


Exec. on linux

    Morae!Q!