Given a starting position ‘k’ and two jump sizes ‘d1’ and ‘d2’, our task is to find the minimum number of jumps needed to reach ‘x’ if it is possible.
At any position P, we are allowed to jump to positions :
- P + d1 and P – d1
- P + d2 and P – d2
Input : k = 10, d1 = 4, d2 = 6 and x = 8 Output : 2 1st step 10 + d1 = 14 2nd step 14 - d2 = 8 Input : k = 10, d1 = 4, d2 = 6 and x = 9 Output : -1 -1 indicates it is not possible to reach x.
In the previous article we discussed a strategy to check whether a list of numbers is reachable by K by making jump of two given lengths.
Here, instead of a list of numbers, we are given a single integer x and if it is reachable from k then the task is to find the minimum number of steps or jumps needed.
We will solve this using Breadth first Search:
- Check if ‘x’ is reachable from k. The number x is reachable from k if it satisfies (x – k) % gcd(d1, d2) = 0.
- If x is reachable :
- Maintain a hash table to store the already visited positions.
- Apply bfs algorithm starting from the position k.
- If you reach position P in ‘stp’ steps, you can reach p+d1 position in ‘stp+1’ steps.
- If position P is the required position ‘x’ then steps taken to reach P is the answer
The image below depicts how the algorithm finds out number of steps needed to reach x = 8 with k = 10, d1 = 4 and d2 = 6.
Below is the implementation of the above approach:
- Reach the numbers by making jumps of two given lengths
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Find the number of jumps to reach X in the number line from zero
- Minimum number of jumps to reach end
- Paths requiring minimum number of jumps to reach end of array
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Count ways to reach end from start stone with at most K jumps at each step
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Count minimum factor jumps required to reach the end of an Array
- Minimum length of jumps to avoid given array of obstacles
- Check if it is possible to reach to the index with value K when start index is given
- Find if two people ever meet after same number of jumps
- Check if it is possible to reach vector B by rotating vector A and adding vector C to it
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Minimum jumps required to group all 1s together in a given Binary string
- Find if it is possible to reach the end through given transitions
- Is it possible to reach N and M from 1 and 0 respectively as per given conditio
- Number of jumps for a thief to cross walls
- Making elements of two arrays same with minimum increment/decrement
- Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.