Minimum moves to reach target on a infinite line | Set 2

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.

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.



Approach :
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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to find minimum moves
// to reach target
#include <bits/stdc++.h>
using namespace std;
  
// Function to find minimum steps
// to reach target
int StepstoReachTarget(int target)
{
    // Handling negatives
    // by symmetry
    target = abs(target);
  
    // Keep moving while sum is
    // smaller i.e calculating n
    int n = ceil((-1.0 + sqrt(1 + 8.0 * target)) / 2);
    int sum = n * (n + 1) / 2;
  
    if (sum == target)
        return n;
  
    int d = sum - target;
  
    // case 1 : d is even
    if ((d & 1) == 0)
        return n;
  
    // d is odd
    else
        return n + ((n & 1) ? 2 : 1);
}
  
// Driver code
int main()
{
    int target = 5;
    cout << StepstoReachTarget(target);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to find minimum moves
// to reach target
import java.lang.*;
  
class GFG {
      
    // Function to find minimum steps
    // to reach target
    static int StepstoReachTarget(int target)
    {
          
        // Handling negatives
        // by symmetry
        target = Math.abs(target);
  
        // Keep moving while sum is
        // smaller i.e calculating n
        int n = (int)Math.ceil((-1.0
              (int)Math.sqrt(1 + 8.0 *
                         target)) / 2);
                           
        int sum = n * (n + 1) / 2;
  
        if (sum == target)
            return n;
  
        int d = sum - target;
  
        // case 1 : d is even
        if ((d & 1) == 0)
            return n;
  
        // d is odd
        else
            return n + ((n & 1) != 0 
                           ? 2 : 1);
    }
  
    // Driver code
    public static void main(String[] arg)
    {
        int target = 5;
        System.out.println(
             StepstoReachTarget(target));
    }
}
  
// This code is contributed by
// Smitha Dinesh Semwal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to find minimum 
# moves to reach target
import math
  
# Function to find minimum 
# steps to reach target
def StepstoReachTarget(target) :
  
    # Handling negatives
    # by symmetry
    target = abs(target)
  
    # Keep moving while sum is
    # smaller i.e calculating n
    n = math.ceil((-1.0 + math.sqrt(1 +
                    8.0 * target)) / 2)
    sum = n * (n + 1) / 2
  
    if (sum == target) :
        return n
  
    d = sum - target
  
    # case 1 : d is even
    if ((d and 1) == 0) :
        return n
  
    # d is odd
    else :
        if(n & 1) :
            return n + 2
        return n + 1
  
# Driver code
target = 5
print (StepstoReachTarget(target))
  
# This code is contributed by 
# Manish Shaw(manishshaw1)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to find minimum moves
// to reach target
using System;
  
class GFG {
      
    // Function to find minimum steps
    // to reach target
    static int StepstoReachTarget(int target)
    {
          
        // Handling negatives
        // by symmetry
        target = Math.Abs(target);
  
        // Keep moving while sum is
        // smaller i.e calculating n
        int n = (int)Math.Ceiling((-1.0 + 
                  (int)Math.Sqrt(1 + 8.0 *
                            target)) / 2);
                          
        int sum = n * (n + 1) / 2;
  
        if (sum == target)
            return n;
  
        int d = sum - target;
  
        // case 1 : d is even
        if ((d & 1) == 0)
            return n;
  
        // d is odd
        else
            return n + ((n & 1) != 0
                        ? 2 : 1);
    }
  
    // Driver code
    public static void Main()
    {
        int target = 5;
        Console.Write(
            StepstoReachTarget(target));
    }
}
  
// This code is contributed by nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to find minimum 
// moves to reach target
  
// Function to find minimum 
// steps to reach target
function StepstoReachTarget($target)
{
    // Handling negatives$
    // by symmetry$
    $target = abs($target);
  
    // Keep moving while sum is
    // smaller i.e calculating n
    $n = ceil((-1.0 + sqrt(1 + 
                8.0 * $target)) / 2);
    $sum = $n * ($n + 1) / 2;
  
    if ($sum == $target)
        return $n;
  
    $d = $sum - $target;
  
    // case 1 : d is even
    if (($d & 1) == 0)
        return n;
  
    // d is odd
    else
        return $n + (($n & 1) ? 2 : 1);
}
  
// Driver code
$target = 5;
echo StepstoReachTarget($target);
  
// This code is contributed by anuj_67.
?>

chevron_right


Output :

5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.