# Check if it is possible to reach a number by making jumps of two given length

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

Examples:

```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.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

• 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 :
1. Maintain a hash table to store the already visited positions.
2. Apply bfs algorithm starting from the position k.
3. If you reach position P in ‘stp’ steps, you can reach p+d1 position in ‘stp+1’ steps.
4. 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:

 `#include ` `using` `namespace` `std; ` ` `  `// Function to perform BFS traversal to ` `// find minimum number of step needed ` `// to reach x from K ` `int` `minStepsNeeded(``int` `k, ``int` `d1, ``int` `d2, ``int` `x) ` `{ ` `    ``// Calculate GCD of d1 and d2 ` `    ``int` `gcd = __gcd(d1, d2); ` ` `  `    ``// If position is not reachable ` `    ``// return -1 ` `    ``if` `((k - x) % gcd != 0) ` `        ``return` `-1; ` ` `  `    ``// Queue for BFS ` `    ``queue > q; ` ` `  `    ``// Hash Table for marking ` `    ``// visited positions ` `    ``unordered_set<``int``> visited; ` ` `  `    ``// we need 0 steps to reach K ` `    ``q.push({ k, 0 }); ` ` `  `    ``// Mark starting position ` `    ``// as visited ` `    ``visited.insert(k); ` ` `  `    ``while` `(!q.empty()) { ` ` `  `        ``int` `s = q.front().first; ` ` `  `        ``// stp is the number of steps ` `        ``// to reach position s ` `        ``int` `stp = q.front().second; ` ` `  `        ``if` `(s == x) ` `            ``return` `stp; ` ` `  `        ``q.pop(); ` ` `  `        ``if` `(visited.find(s + d1) == visited.end()) { ` ` `  `            ``// if position not visited ` `            ``// add to queue and mark visited ` `            ``q.push({ s + d1, stp + 1 }); ` ` `  `            ``visited.insert(s + d1); ` `        ``} ` ` `  `        ``if` `(visited.find(s + d2) == visited.end()) { ` `            ``q.push({ s + d2, stp + 1 }); ` `            ``visited.insert(s + d2); ` `        ``} ` ` `  `        ``if` `(visited.find(s - d1) == visited.end()) { ` `            ``q.push({ s - d1, stp + 1 }); ` `            ``visited.insert(s - d1); ` `        ``} ` `        ``if` `(visited.find(s - d2) == visited.end()) { ` `            ``q.push({ s - d2, stp + 1 }); ` `            ``visited.insert(s - d2); ` `        ``} ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `k = 10, d1 = 4, d2 = 6, x = 8; ` ` `  `    ``cout << minStepsNeeded(k, d1, d2, x); ` ` `  `    ``return` `0; ` `} `

Output:

```2
```

My Personal Notes arrow_drop_up