Given a number line from -infinity to +infinity. You start at 0 and can go either to the left or to the right. The condition is that in i’th move, you take i steps.
- Find if you can reach a given number x
- Find the most optimal way to reach a given number x, if we can indeed reach it. For example, 3 can be reached in 2 steps, (0, 1) (1, 3) and 4 can be reached in 3 steps (0, -1), (-1, 1) (1, 4).
Source: Flipkart Interview Question
The important thing to note is we can reach any destination as it is always possible to make a move of length 1. At any step i, we can move forward i, then backward i + 1.
Below is a recursive solution suggested by Arpit Thapar here.
- Since distance of + 5 and – 5 from 0 is same, hence we find answer for absolute value of destination.
- We use a recursive function which takes as arguments:
- Source Vertex
- Value of last step taken
- Destination
- If at any point source vertex = destination; return number of steps.
- Otherwise we can go in both of the possible directions. Take the minimum of steps in both cases.
- From any vertex we can go to :
- (current source + last step +1) and
- (current source – last step -1)
- If at any point, absolute value of our position exceeds the absolute value of our destination then it is intuitive that the shortest path is not possible from here. Hence we make the value of steps INT_MAX, so that when i take the minimum of both possibilities, this one gets eliminated.
If we don’t use this last step, the program enters into an INFINITE recursion and gives RUN TIME ERROR.
Below is the implementation of above idea. Note that the solution only counts steps.
C++
#include<bits/stdc++.h>
using namespace std;
int steps( int source, int step, int dest)
{
if ( abs (source) > (dest))
return INT_MAX;
if (source == dest) return step;
int pos = steps(source + step + 1,
step + 1, dest);
int neg = steps(source - step - 1,
step + 1, dest);
return min(pos, neg);
}
int main()
{
int dest = 11;
cout << "No. of steps required to reach "
<< dest << " is "
<< steps(0, 0, dest);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int steps( int source, int step,
int dest)
{
if (Math.abs(source) > (dest))
return Integer.MAX_VALUE;
if (source == dest)
return step;
int pos = steps(source + step + 1 ,
step + 1 , dest);
int neg = steps(source - step - 1 ,
step + 1 , dest);
return Math.min(pos, neg);
}
public static void main(String[] args)
{
int dest = 11 ;
System.out.println( "No. of steps required" +
" to reach " + dest +
" is " + steps( 0 , 0 , dest));
}
}
|
Python3
import sys
def steps(source, step, dest):
if ( abs (source) > (dest)) :
return sys.maxsize
if (source = = dest):
return step
pos = steps(source + step + 1 ,
step + 1 , dest)
neg = steps(source - step - 1 ,
step + 1 , dest)
return min (pos, neg)
dest = 11 ;
print ( "No. of steps required" ,
" to reach " ,dest ,
" is " , steps( 0 , 0 , dest));
|
C#
using System;
class GFG
{
static int steps( int source, int step,
int dest)
{
if (Math.Abs(source) > (dest))
return int .MaxValue;
if (source == dest)
return step;
int pos = steps(source + step + 1,
step + 1, dest);
int neg = steps(source - step - 1,
step + 1, dest);
return Math.Min(pos, neg);
}
public static void Main()
{
int dest = 11;
Console.WriteLine( "No. of steps required" +
" to reach " + dest +
" is " + steps(0, 0, dest));
}
}
|
PHP
<?php
function steps( $source , $step , $dest )
{
if ( abs ( $source ) > ( $dest ))
return PHP_INT_MAX;
if ( $source == $dest )
return $step ;
$pos = steps( $source + $step + 1,
$step + 1, $dest );
$neg = steps( $source - $step - 1,
$step + 1, $dest );
return min( $pos , $neg );
}
$dest = 11;
echo "No. of steps required to reach " ,
$dest , " is " , steps(0, 0, $dest );
?>
|
Javascript
<script>
function steps(source, step, dest)
{
if (Math.abs(source) > (dest))
return Number.MAX_SAFE_INTEGER;
if (source == dest) return step;
let pos = steps(source + step + 1,
step + 1, dest);
let neg = steps(source - step - 1,
step + 1, dest);
return Math.min(pos, neg);
}
let dest = 11;
document.write( "No. of steps required to reach "
+ dest + " is "
+ steps(0, 0, dest));
</script>
|
Output
No. of steps required to reach 11 is 5
Time Complexity : O(2^n)
Auxiliary Space : O(2^n)
Thanks to Arpit Thapar for providing above algorithm and implementation.
Optimized Solution : Find minimum moves to reach target on an infinite line
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Aug, 2022
Like Article
Save Article