Minimum moves to reach target on a infinite line | Set 2
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.
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 :
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.
C++
#include <bits/stdc++.h>
using namespace std;
int StepstoReachTarget( int target)
{
target = abs (target);
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;
if ((d & 1) == 0)
return n;
else
return n + ((n & 1) ? 2 : 1);
}
int main()
{
int target = 5;
cout << StepstoReachTarget(target);
return 0;
}
|
Java
import java.lang.*;
class GFG {
static int StepstoReachTarget( int target)
{
target = Math.abs(target);
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;
if ((d & 1 ) == 0 )
return n;
else
return n + ((n & 1 ) != 0 ? 2 : 1 );
}
public static void main(String[] arg)
{
int target = 5 ;
System.out.println(StepstoReachTarget(target));
}
}
|
Python3
import math
def StepstoReachTarget(target):
target = abs (target)
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
if (( int (d) & 1 ) = = 0 ):
return n
else :
if ( int (d) & 1 ):
return n + 2
return n + 1
target = 5
print (StepstoReachTarget(target))
|
C#
using System;
class GFG {
static int StepstoReachTarget( int target)
{
target = Math.Abs(target);
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;
if ((d & 1) == 0)
return n;
else
return n + ((n & 1) != 0 ? 2 : 1);
}
public static void Main()
{
int target = 5;
Console.Write(StepstoReachTarget(target));
}
}
|
PHP
<?php
function StepstoReachTarget( $target )
{
$target = abs ( $target );
$n = ceil ((-1.0 + sqrt(1 +
8.0 * $target )) / 2);
$sum = $n * ( $n + 1) / 2;
if ( $sum == $target )
return $n ;
$d = $sum - $target ;
if (( $d & 1) == 0)
return n;
else
return $n + (( $n & 1) ? 2 : 1);
}
$target = 5;
echo StepstoReachTarget( $target );
?>
|
Javascript
<script>
function StepstoReachTarget(target)
{
target = Math.abs(target);
let n = Math.ceil((-1.0 +
Math.sqrt(1 + 8.0 * target)) / 2);
let sum = n * (n + 1) / 2;
if (sum == target)
return n;
let d = sum - target;
if ((d & 1) == 0)
return n;
else
return n + ((n & 1) != 0 ? 2 : 1);
}
let target = 5;
document.write(StepstoReachTarget(target));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...