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.
a) Find if you can reach a given number x
b) 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 think 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.
1) Since distance of + 5 and – 5 from 0 is same, hence we find answer for absolute value of destination.
2) We use a recursive function which takes as arguments:
i) Source Vertex
ii) Value of last step taken
3) If at any point source vertex = destination; return number of steps.
4) 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)
5) 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.
No. of steps required to reach 11 is 5
Thanks to Arpit Thapar for providing above algorithm and implementation.
Optimized Solution : Find minimum moves to reach target on an infinite line
This article is contributed by Abhay. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Minimum Initial Points to Reach Destination
- Find the minimum cost to reach destination using a train
- Minimum cells required to reach destination with jumps equal to cell values
- Minimum steps required to reach the end of a matrix | Set 2
- Minimum steps to reach target by a Knight | Set 1
- Minimum steps to reach target by a Knight | Set 2
- Minimum steps to reach end of array under constraints
- Minimum steps to reach any of the boundary edges of a matrix | Set 1
- Find minimum steps required to reach the end of a matrix | Set 2
- Minimum steps to reach any of the boundary edges of a matrix | Set-2
- Find minimum steps required to reach the end of a matrix | Set - 1
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Number of decisions to reach destination
- Count number of ways to reach destination in a Maze using BFS
- Count number of ways to reach destination in a Maze