Find minimum moves to reach target on an infinite line | C # program to find minimum moves to reach target if we can move i steps in i - th move . ; Handling negatives by symmetry ; Keep moving while sum is smaller or difference is odd . ; Driver code
using System ; class GFG { static int reachTarget ( int target ) { target = Math . Abs ( target ) ; int sum = 0 , step = 0 ; while ( sum < target || ( sum - target ) % 2 != 0 ) { step ++ ; sum += step ; } return step ; } public static void Main ( ) { int target = 5 ; Console . WriteLine ( reachTarget ( target ) ) ; } }