Given a target position on 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.
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 sum is greater than target. Find the difference by how much steps index is ahead of target, i.e sum – target.
Case 1 : Difference is even then answer 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 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 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 steps to reach a destination
- Minimum time to burn a Tree starting from a Leaf node
- Minimum number of increment-other operations to make all array elements equal.
- Minimum number of items to be delivered
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Flipkart Interview Experience | Set 25
- Add two numbers represented by linked lists | Set 1
- Add two numbers represented by linked lists | Set 2
- Merge k sorted arrays | Set 1
- Flipkart Interview | Set 1 (For SDE 2)
- Flipkart Interview | Set 2 (For SDE 1)
- Flipkart Interview | Set 3
- Flipkart Interview | Set 4 (For SDE-1)
- Print a Binary Tree in Vertical Order | Set 1
- Flipkart Interview | Set 5 (Off-Campus for SDE-1)
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Flipkart Interview | Set 6
- Flipkart Interview | Set 7 (For SDE II)
- Flipkart Interview | Set 8 (For SDE 1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.