# Find minimum moves to reach target on an infinite line

Given a target position on infinite number line, i.e -infinity to +infinity. Starting form 0 you have to reach the target by moving as described : In ith move you can take i steps forward or backward. Find the minimum number of moves require to reach the target.

**Examples :**

Input : target = 3 Output : 2 Explanation: On the first move we step from 0 to 1. On the second step we step from 1 to 3. Input: target = 2 Output: 3 Explanation: On the first move we step from 0 to 1. On the second move we step from 1 to -1. On the third move we step from -1 to 2.

We have discussed a naive recursive solution in below post.

Minimum steps to reach a destination

If target is negative, we can take it as positive because we start from 0 in symmetrical way.

Idea is to move in one direction as long as possible, this will give minimum moves. Starting at 0 first move takes us to 1, second move takes us to 3 (1+2) position, third move takes us to 6 (1+2+3) position, ans so on; So for finding target we keep on adding moves until we find the nth move such that 1+2+3+…+n>=target. Now if sum (1+2+3+…+n) is equal to target the our job is done, i.e we’ll need n moves to reach target. Now next case where **sum is greater than target**. Find the difference by how much we are ahead, i.e sum – target. Let the difference be d = sum – target.

If we take the i-th move backward then the new sum will become (sum – 2i), i.e 1+2+3+…-x+x+1…+n. Now if sum-2i = target then our job is done. Since, sum – target = 2i, i.e difference should be even as we will get an integer i flipping which will give the answer. So following cases arise.

**Case 1 :** Difference is even then answer is n, (because we will always get a move flipping which will lead to target).

**Case 2 :** Difference is odd, then we take one more step, i.e add n+1 to sum and now again take the difference. If difference is even the n+1 is the answer else we would have to take one more move and this will certainly make the difference even then answer will be n+2.

Explanation : Since difference is odd. Target is either odd or even.

case 1: n is even (1+2+3+…+n) then adding n+1 makes the difference even.

case 2: n is odd then adding n+1 doesn’t makes difference even so we would have to take one more move, so n+2.

**Example: **

target = 5.

we keep on taking moves until we reach target or we just cross it.

sum = 1 + 2 + 3 = 6 > 5, step = 3.

Difference = 6 – 5 = 1. Since the difference is an odd value, we will not reach the target by flipping any move from +i to -i. So we increase our step. We need to increase step by 2 to get an even difference (since n is odd and target is also odd). Now that we have an even difference, we can simply switch any move to the left (i.e. change + to -) as long as the summation of the changed value equals to half of the difference. We can switch 1 and 4 or 2 and 3 or 5.

## C++

`// CPP program to find minimum moves to ` `// reach target if we can move i steps in ` `// i-th move. ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `reachTarget(` `int` `target) ` `{ ` ` ` `// Handling negatives by symmetry ` ` ` `target = ` `abs` `(target); ` ` ` ` ` `// Keep moving while sum is smaller or difference ` ` ` `// is odd. ` ` ` `int` `sum = 0, step = 0; ` ` ` `while` `(sum < target || (sum - target) % 2 != 0) { ` ` ` `step++; ` ` ` `sum += step; ` ` ` `} ` ` ` `return` `step; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `target = 5; ` ` ` `cout << reachTarget(target); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum ` `//moves to reach target if we can ` `// move i steps in i-th move. ` `import` `java.io.*; ` `import` `java.math.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `reachTarget(` `int` `target) ` ` ` `{ ` ` ` `// Handling negatives by symmetry ` ` ` `target = Math.abs(target); ` ` ` ` ` `// Keep moving while sum is smaller ` ` ` `// or difference is odd. ` ` ` `int` `sum = ` `0` `, step = ` `0` `; ` ` ` ` ` `while` `(sum < target || (sum - target) % ` `2` ` ` `!= ` `0` `) { ` ` ` `step++; ` ` ` `sum += step; ` ` ` `} ` ` ` `return` `step; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `target = ` `5` `; ` ` ` `System.out.println(reachTarget(target)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nikita tiwari. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find minimum ` `# moves to reach target if we can ` `# move i steps in i-th move. ` ` ` ` ` `def` `reachTarget(target) : ` ` ` ` ` `# Handling negatives by symmetry ` ` ` `target ` `=` `abs` `(target) ` ` ` ` ` `# Keep moving while sum is ` ` ` `# smaller or difference is odd. ` ` ` `sum` `=` `0` ` ` `step ` `=` `0` ` ` `while` `(` `sum` `< target ` `or` `(` `sum` `-` `target) ` `%` ` ` `2` `!` `=` `0` `) : ` ` ` `step ` `=` `step ` `+` `1` ` ` `sum` `=` `sum` `+` `step ` ` ` ` ` `return` `step ` ` ` ` ` `# Driver code ` `target ` `=` `5` `print` `(reachTarget(target)) ` ` ` ` ` `# This code is contributed by Nikita Tiwari ` |

*chevron_right*

*filter_none*

## C#

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

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// minimum moves to reach ` `// target if we can move i ` `// steps in i-th move. ` ` ` `function` `reachTarget(` `$target` `) ` `{ ` ` ` `// Handling negatives ` ` ` `// by symmetry ` ` ` `$target` `= ` `abs` `(` `$target` `); ` ` ` ` ` `// Keep moving while sum is ` ` ` `// smaller or difference is odd. ` ` ` `$sum` `= 0; ` `$step` `= 0; ` ` ` `while` `(` `$sum` `< ` `$target` `or` ` ` `(` `$sum` `- ` `$target` `) % 2 != 0) ` ` ` `{ ` ` ` `$step` `++; ` ` ` `$sum` `+= ` `$step` `; ` ` ` `} ` ` ` `return` `$step` `; ` `} ` ` ` `// Driver code ` `$target` `= 5; ` `echo` `reachTarget(` `$target` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

5

## Recommended Posts:

- Minimum moves to reach target on a infinite line | Set 2
- Minimum number of moves to reach N starting from (1, 1)
- Minimum number of moves required to reach the destination by the king in a chess board
- Minimum time to reach a point with +t and -t moves at time t
- Find the number of jumps to reach X in the number line from zero
- Find the minimum number of steps to reach M from N
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Minimum steps needed to cover a sequence of points on an infinite grid
- Find the sum of infinite series 1^2.x^0 + 2^2.x^1 + 3^2.x^2 + 4^2.x^3 +.......
- Minimum number of given moves required to make N divisible by 25
- Find if the given number is present in the infinite sequence or not
- Find the count of unvisited indices in an infinite array
- Minimum step to reach one
- Minimum number of jumps to reach end

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.