Open In App

Jump Game III Problem

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of non-negative integers size N, the task is to start from index X and check if we can reach any index with value 0. For any index i, we can jump to i + arr[i] or i - arr[i].

Note: We cannot jump outside the array at any time.

Examples:

Input: arr[] = {4, 2, 3, 0, 3, 1, 2}, X = 5
Output: true
Explanation: It is possible to reach index 3 from index 5.

  • Start from index 5, arr[5] = 1 so jump to index 4.
  • From index 4, arr[4] = 3 so jump to index 1.
  • From index 1, arr[1] = 2 so jump to index 3.

Input: arr[] = {3, 0, 2, 1, 2}, X = 2
Output: false
Explanation: It is impossible to reach any index with value 0.

Approach: To solve the problem, follow the below idea:

The problem can be solved using Breadth-First-Search. Maintain a dist[] array which stores the minimum jumps to reach any index. We can start from the index X and check for index X + arr[X] and X - arr[X]. If the distance to reach those indices are minimum so far, then update the distance and push those indices to the queue. We continue to traverse the array arr[] till q does not become empty. Now, we can simply traverse over all the indices and check for any index with value 0 with distance != INF. If such an index is found, return true else return false.

Step-by-step algorithm:

  • Maintain a distance array dist[] which stores minimum jumps to store minimum jumps to reach any index.
  • Maintain a queue to run BFS starting from index X.
  • Initialize dist[X] = 0 and push X to the queue.
  • While queue is not empty,
    • Pop the front element, say idx from the queue.
    • Find the index left = idx - arr[idx] and check if the distance to reach the left is greater than current distance. If yes, then update dist[left] and push the left index to the queue.
    • Find the index right = idx + arr[idx] and check if the distance to reach the right is greater than current distance. If yes, then update dist[right] and push the right index to the queue.
  • After iterating over all the indices and check for any index with value 0 with distance != INF. If such an index is found, return true else return false.

Below is the implementation of the above approach:

C++
#include <bits/stdc++.h>
using namespace std;

// function to check if it is possible to reach index with
// value = 0
bool canReach(vector<int>& arr, int X)
{
    int n = arr.size();
    vector<int> dist(n, 1e9);
    // Initialize dist[X] = 0
    dist[X] = 0;
    // queue for BFS
    queue<int> q;
    q.push(X);
    while (!q.empty()) {
        int idx = q.front();
        q.pop();
        // Left jump
        int left = idx >= arr[idx] ? idx - arr[idx] : -1;
        // Right jump
        int right
            = idx + arr[idx] < n ? idx + arr[idx] : -1;
        // Check if it is beneficial to take the left jump
        if (left != -1 && dist[left] > dist[idx] + 1) {
            dist[left] = dist[idx] + 1;
            q.push(left);
        }
        // Check if it is beneficial to take the right jump
        if (right != -1 && dist[right] > dist[idx] + 1) {
            dist[right] = dist[idx] + 1;
            q.push(right);
        }
    }
    // Check if it possible to reach any index with value =
    // 0
    for (int i = 0; i < n; i++) {
        if (arr[i] == 0 && dist[i] != 1e9)
            return true;
    }
    return false;
}

int main()
{
    vector<int> arr = { 4, 2, 3, 0, 3, 1, 2 };
    int X = 5;
    cout << canReach(arr, X);
    return 0;
}
Java
import java.util.*;

public class Main {

    public static boolean canReach(int[] arr, int X) {
        int n = arr.length;
        int[] dist = new int[n];
        Arrays.fill(dist, Integer.MAX_VALUE);
        // Initialize dist[X] = 0
        dist[X] = 0;
        // Queue for BFS
        Queue<Integer> q = new LinkedList<>();
        q.offer(X);
        while (!q.isEmpty()) {
            int idx = q.poll();
            // Left jump
            int left = idx >= arr[idx] ? idx - arr[idx] : -1;
            // Right jump
            int right = idx + arr[idx] < n ? idx + arr[idx] : -1;
            // Check if it is beneficial to take the left jump
            if (left != -1 && dist[left] > dist[idx] + 1) {
                dist[left] = dist[idx] + 1;
                q.offer(left);
            }
            // Check if it is beneficial to take the right jump
            if (right != -1 && dist[right] > dist[idx] + 1) {
                dist[right] = dist[idx] + 1;
                q.offer(right);
            }
        }
        // Check if it's possible to reach any index with value = 0
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0 && dist[i] != Integer.MAX_VALUE) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = { 4, 2, 3, 0, 3, 1, 2 };
        int X = 5;
        System.out.println(canReach(arr, X));
    }
}

// This code is contributed by Shivam Gupta

Output
1

Time Complexity: O(N)
Auxiliary Space: O(N)


Similar Reads