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.
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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 codeint main(){ int target = 5; cout << reachTarget(target); return 0;} |
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. |
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 codetarget = 5print(reachTarget(target))# This code is contributed by Nikita Tiwari |
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. |
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.?> |
Javascript
<script>// JavaScript 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 = Math.abs(target); // Keep moving while sum is smaller // or difference is odd. let sum = 0, step = 0; while (sum < target || (sum - target) % 2 != 0) { step++; sum += step; } return step; }// Driver code let target = 5; document.write(reachTarget(target));</script> |
Output :
5


