Given a target position on the infinite number line, (-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 required to reach the target.
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.
The idea is similar to discussed in O(n) approach here.
Keep adding sum = 1 + 2 + .. + n >= target. Solving this quadratic equation gives the smallest n such that sum >= target, i.e solving for n in n(n+1) / 2 – target >= 0 gives smallest n.
If sum == target, answer is n. Now next case where the sum is greater than the target. Find the difference by how many steps index is ahead of target, i.e sum — target.
Case 1: Difference is even then answered is n, (because there will always a move flipping which will lead to target).
Case 2: Difference is odd, then take one more step, i.e add n+1 to sum and now again take the difference. If the difference is even the n+1 is the answer else take one more move and this will certainly make the difference even then answer will be n + 2.
Explanation: Since the 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, take one more move, i.e., n+2.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find minimum moves to reach target on an infinite line
- Minimum time to reach a point with +t and -t moves at time t
- Minimum number of moves required to reach the destination by the king in a chess board
- Minimum number of moves to reach N starting from (1, 1)
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Count of all possible ways to reach a target by a Knight
- Total position where king can reach on a chessboard in exactly M moves
- Expected number of moves to reach the end of a board | Dynamic programming
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Minimum steps needed to cover a sequence of points on an infinite grid
- Find the number of jumps to reach X in the number line from zero
- Measure one litre using two vessels and infinite water supply
- Sum of series M/1 + (M+P)/2 + (M+2*P)/4 + (M+3*P)/8......up to infinite
- Find the sum of infinite series 1^2.x^0 + 2^2.x^1 + 3^2.x^2 + 4^2.x^3 +.......
- Find if the given number is present in the infinite sequence or not
- Find the count of unvisited indices in an infinite array
- Slope of the line parallel to the line with the given slope
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Minimum number of given moves required to make N divisible by 25
- Minimum moves taken to move coin of each cell to any one cell of Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.