GFG App
Open App
Browser
Continue

Given an array of numbers where each element represents the max number of jumps that can be made forward from that element. For each array element, count the number of ways jumps can be made from that element to reach the end of the array. If an element is 0, then a move cannot be made through that element. The element that cannot reach the end should have a count “-1“.

Examples:

Input: {3, 2, 0, 1}
Output: 2 1 -1 0
Explanation:

• For 3 number of steps or jumps that can be taken are 1, 2 or 3. The different ways are:
3 -> 2 -> 1
3 -> 1
• For 2 number of steps or jumps that can be taken are 1, or 2. The different ways are:
2 -> 1
• For 0 number of steps or jumps that can be taken are 0. One cannot move forward from this point.
• For 1 number of steps or jumps that can be taken are 1. But the element is at the end so no jump is required.

Input: {1, 3, 5, 8, 9, 1, 0, 7, 6, 8, 9}
Output: 52 52 28 16 8 -1 -1 4 2 1 0

This problem is a variation of the Minimum number of jumps to reach end(Method 3). Here we need to count all the ways to reach the end from every cell.
The solution is a modified version of the solution to the problem of the Minimum number of jumps to reach end(Method 3)
This problem aims to count the different ways to jump from each element to reach the end. For each element, the count is being calculated by adding the counts of all those forward elements that can reach the end and to which the current element could reach + 1(if the element can directly reach the end).

Algorithm:

```countWays(arr, n)
Initialize array count_jump[n] = {0}

count_jump[n-1] = 0
for i = n-2 to 0
if arr[i] >= (n-i-1)
count_jump[i]++
for j=i+1; j < n-1 && j <= arr[i]+i; i++
if count_jump[j] != -1
count_jump[i] += count_jump[j]
if count_jump[i] == 0
count_jump[i] = -1

for i = 0 to n-1
print count_jump[i]```

Below is the implementation of the above approach:

## C++

 `// C++ implementation to count number` `// of ways to jump to reach end` `#include ` `using` `namespace` `std;`   `// function to count ways to jump to` `// reach end for each array element` `void` `countWaysToJump(``int` `arr[], ``int` `n)` `{` `    ``// count_jump[i] store number of ways` `    ``// arr[i] can reach to the end` `    ``int` `count_jump[n];` `    ``memset``(count_jump, 0, ``sizeof``(count_jump));`   `    ``// Last element does not require to jump.` `    ``// Count ways to jump for remaining` `    ``// elements` `    ``for` `(``int` `i=n-2; i>=0; i--)` `    ``{` `        ``// if the element can directly` `        ``// jump to the end` `        ``if` `(arr[i] >= n - i - 1)` `            ``count_jump[i]++;`   `        ``// add the count of all the elements` `        ``// that can reach to end and arr[i] can` `        ``// reach to them` `        ``for` `(``int` `j=i+1; j < n-1 && j <= arr[i] + i; j++)`   `            ``// if element can reach to end then add` `            ``// its count to count_jump[i]` `            ``if` `(count_jump[j] != -1)` `                 ``count_jump[i] += count_jump[j];`   `        ``// if arr[i] cannot reach to the end` `        ``if` `(count_jump[i] == 0)` `            ``count_jump[i] = -1;` `    ``}`   `    ``// print count_jump for each` `    ``// array element` `    ``for` `(``int` `i=0; i

## Java

 `// Java implementation to count number` `// of ways to jump to reach end` `import` `java.util.Arrays;`   `class` `GFG {` `    `  `    ``// function to count ways to jump to` `    ``// reach end for each array element` `    ``static` `void` `countWaysToJump(``int` `arr[], ``int` `n)` `    ``{` `        `  `        ``// count_jump[i] store number of ways` `        ``// arr[i] can reach to the end` `        ``int` `count_jump[] = ``new` `int``[n];` `        ``Arrays.fill(count_jump, ``0``);` `    `  `        ``// Last element does not require to jump.` `        ``// Count ways to jump for remaining` `        ``// elements` `        ``for` `(``int` `i = n-``2``; i >= ``0``; i--)` `        ``{` `            `  `            ``// if the element can directly` `            ``// jump to the end` `            ``if` `(arr[i] >= n - i - ``1``)` `                ``count_jump[i]++;` `    `  `            ``// add the count of all the elements` `            ``// that can reach to end and arr[i] can` `            ``// reach to them` `            ``for` `(``int` `j = i+``1``; j < n-``1` `&& j <= arr[i] + i; j++)` `    `  `                ``// if element can reach to end then add` `                ``// its count to count_jump[i]` `                ``if` `(count_jump[j] != -``1``)` `                    ``count_jump[i] += count_jump[j];` `    `  `            ``// if arr[i] cannot reach to the end` `            ``if` `(count_jump[i] == ``0``)` `                ``count_jump[i] = -``1``;` `        ``}` `    `  `        ``// print count_jump for each` `        ``// array element` `        ``for` `(``int` `i = ``0``; i < n; i++)` `            ``System.out.print(count_jump[i] + ``" "``);` `    ``}` `    `  `    ``//driver code` `    ``public` `static` `void` `main (String[] args)` `    ``{` `        `  `        ``int` `arr[] = {``1``, ``3``, ``5``, ``8``, ``9``, ``1``, ``0``, ``7``, ``6``, ``8``, ``9``};` `        ``int` `n = arr.length;` `        `  `        ``countWaysToJump(arr, n);` `    ``}` `}`   `// This code is contributed by Anant Agarwal.`

## Python3

 `# Python3 implementation to count ` `# number of ways to jump to reach end`   `# Function to count ways to jump to` `# reach end for each array element` `def` `countWaysToJump(arr, n):`   `    ``# count_jump[i] store number of ways` `    ``# arr[i] can reach to the end` `    ``count_jump ``=` `[``0` `for` `i ``in` `range``(n)]`   `    ``# Last element does not require ` `    ``# to jump. Count ways to jump for ` `    ``# remaining elements` `    ``for` `i ``in` `range``(n ``-` `2``, ``-``1``, ``-``1``):` `    `  `        ``# if the element can directly` `        ``# jump to the end` `        ``if` `(arr[i] >``=` `n ``-` `i ``-` `1``):` `            ``count_jump[i] ``+``=` `1`   `        ``# Add the count of all the elements` `        ``# that can reach to end and arr[i] ` `        ``# can reach to them` `        ``j ``=` `i ``+` `1` `        ``while``(j < n``-``1` `and` `j <``=` `arr[i] ``+` `i):`   `            ``# if element can reach to end then ` `            ``# add its count to count_jump[i]` `            ``if` `(count_jump[j] !``=` `-``1``):` `                ``count_jump[i] ``+``=` `count_jump[j]` `            ``j ``+``=` `1` `            `  `        ``# if arr[i] cannot reach to the end` `        ``if` `(count_jump[i] ``=``=` `0``):` `            ``count_jump[i] ``=` `-``1` `    `    `    ``# print count_jump for each` `    ``# array element` `    ``for` `i ``in` `range``(n):` `        ``print``(count_jump[i], end ``=` `" "``)`   `# Driver code` `arr ``=` `[``1``, ``3``, ``5``, ``8``, ``9``, ``1``, ``0``, ``7``, ``6``, ``8``, ``9``]` `n ``=` `len``(arr)` `countWaysToJump(arr, n)`   `# This code is contributed by Anant Agarwal.`

## Javascript

 ``

## C#

 `// C# implementation to count number ` `// of ways to jump to reach end ` `using` `System;`   `class` `GFG { ` `    `  `    ``// function to count ways to jump to ` `    ``// reach end for each array element ` `    ``static` `void` `countWaysToJump(``int``[] arr, ``int` `n) ` `    ``{ ` `        `  `        ``// count_jump[i] store number of ways ` `        ``// arr[i] can reach to the end ` `        ``int``[] count_jump = ``new` `int``[n];` `        `  `        ``for``(``int` `i = 0; i < n; i++)` `            ``count_jump[i] = 0;` `        `  `    `  `        ``// Last element does not require to jump. ` `        ``// Count ways to jump for remaining ` `        ``// elements ` `        ``for` `(``int` `i = n-2; i >= 0; i--) ` `        ``{ ` `            `  `            ``// if the element can directly ` `            ``// jump to the end ` `            ``if` `(arr[i] >= n - i - 1) ` `                ``count_jump[i]++; ` `    `  `            ``// add the count of all the elements ` `            ``// that can reach to end and arr[i] can ` `            ``// reach to them ` `            ``for` `(``int` `j = i+1; j < n-1 && j <= arr[i] + i; j++) ` `    `  `                ``// if element can reach to end then add ` `                ``// its count to count_jump[i] ` `                ``if` `(count_jump[j] != -1) ` `                    ``count_jump[i] += count_jump[j]; ` `    `  `            ``// if arr[i] cannot reach to the end ` `            ``if` `(count_jump[i] == 0) ` `                ``count_jump[i] = -1; ` `        ``} ` `    `  `        ``// print count_jump for each ` `        ``// array element ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``Console.Write(count_jump[i] + ``" "``); ` `    ``} ` `    `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int``[] arr = {1, 3, 5, 8, 9,` `                 ``1, 0, 7, 6, 8, 9}; ` `        ``int` `n = arr.Length; ` `        ``countWaysToJump(arr, n); ` `    ``} ` `} `   `// This code is contributed by ChitraNayal`

## PHP

 `= 0; ``\$i``--)` `    ``{` `        ``// if the element can directly` `        ``// jump to the end` `        ``if` `(``\$arr``[``\$i``] >= ``\$n` `- ``\$i` `- 1)` `            ``\$count_jump``[``\$i``]++;`   `        ``// add the count of all the elements` `        ``// that can reach to end and arr[i] ` `        ``// can reach to them` `        ``for` `(``\$j` `= ``\$i` `+ 1; ``\$j` `< ``\$n` `- 1 &&` `             ``\$j` `<= ``\$arr``[``\$i``] + ``\$i``; ``\$j``++)`   `            ``// if element can reach to end then ` `            ``// add its count to count_jump[i]` `            ``if` `(``\$count_jump``[``\$j``] != -1)` `                ``\$count_jump``[``\$i``] += ``\$count_jump``[``\$j``];`   `        ``// if arr[i] cannot reach to the end` `        ``if` `(``\$count_jump``[``\$i``] == 0)` `            ``\$count_jump``[``\$i``] = -1;` `    ``}`   `    ``// print count_jump for each` `    ``// array element` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++)` `        ``echo` `\$count_jump``[``\$i``] . ``" "``;` `}`   `// Driver Code` `\$arr` `= ``array``(1, 3, 5, 8, 9, 1,` `                ``0, 7, 6, 8, 9);` `\$n` `= ``count``(``\$arr``);` `countWaysToJump(``\$arr``, ``\$n``);`   `// This code is contributed by Rajput-Ji` `?>`

Output

`52 52 28 16 8 -1 -1 4 2 1 0 `

Time Complexity: O(n2), Where n is the size of the given array.
Auxiliary Space: O(n), As we are creating count_jump array of size n.

Approach 2: Recursion with Memoization

•    Create a memoization table of size n+1, where n is the number of cells in the given array. Initialize all values in the table to -1.
•    Define a recursive function, countWays(arr, i, n, memo) where arr is the input array, i is the current index, n is the total number of cells, and memo is the memoization table.
•    Check if the value of memo[i] is not -1. If it is not -1, return memo[i].
•    If i is equal to n-1, return 1, as there is only one way to reach the end from the last cell.
•    If arr[i] is 0, return 0 as we cannot jump from a cell with value 0.
•    Otherwise, for each valid jump length j from the current cell, call the countWays function recursively with the new index i+j.
•    Add up all the values returned by the recursive calls, and store the result in memo[i].
•    Return memo[i].
•    Call the countWays function with initial parameters arr, 0, n, memo.
•    Return the result obtained from the function call.

## C++

 `#include ` `using` `namespace` `std;`   `int` `jumpWays(``int` `arr[], ``int` `n, ``int` `pos, ``int` `dp[]) {` `    ``// Base case` `    ``if``(pos == n-1) {` `        ``return` `1;` `    ``}` `    ``if``(pos >= n) {` `        ``return` `0;` `    ``}` `    ``// Memoization` `    ``if``(dp[pos] != -1) {` `        ``return` `dp[pos];` `    ``}` `    ``int` `ans = 0;` `    ``for``(``int` `i = 1; i <= arr[pos]; i++) {` `        ``ans += jumpWays(arr, n, pos + i, dp);` `    ``}` `    ``dp[pos] = ans;` `    ``return` `ans;` `}`   `int` `main() {` `    ``int` `arr[] = {1, 3, 5, 8, 9, 1, 0, 7, 6, 8, 9};` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``);` `    ``int` `dp[n];` `    ``memset``(dp, -1, ``sizeof``(dp));` `    ``cout << jumpWays(arr, n, 0, dp) << endl;` `    ``return` `0;` `}`

## Java

 `import` `java.util.Arrays;`   `public` `class` `Main {`   `  ``public` `static` `int` `jumpWays(``int``[] arr, ``int` `n, ``int` `pos, ``int``[] dp) ` `  ``{`   `    ``// Base case` `    ``if``(pos == n-``1``) {` `      ``return` `1``;` `    ``}` `    ``if``(pos >= n) {` `      ``return` `0``;` `    ``}`   `    ``// Memoization` `    ``if``(dp[pos] != -``1``) {` `      ``return` `dp[pos];` `    ``}` `    ``int` `ans = ``0``;` `    ``for``(``int` `i = ``1``; i <= arr[pos]; i++) {` `      ``ans += jumpWays(arr, n, pos + i, dp);` `    ``}` `    ``dp[pos] = ans;` `    ``return` `ans;` `  ``}`   `  ``public` `static` `void` `main(String[] args) {` `    ``int``[] arr = {``1``, ``3``, ``5``, ``8``, ``9``, ``1``, ``0``, ``7``, ``6``, ``8``, ``9``};` `    ``int` `n = arr.length;` `    ``int``[] dp = ``new` `int``[n];` `    ``Arrays.fill(dp, -``1``);` `    ``System.out.println(jumpWays(arr, n, ``0``, dp));` `  ``}` `}`

## Python3

 `def` `jumpWays(arr, n, pos, dp):` `    ``# Base case` `    ``if` `pos ``=``=` `n``-``1``:` `        ``return` `1` `    ``if` `pos >``=` `n:` `        ``return` `0` `    ``# Memoization` `    ``if` `dp[pos] !``=` `-``1``:` `        ``return` `dp[pos]` `    ``ans ``=` `0` `    ``for` `i ``in` `range``(``1``, arr[pos]``+``1``):` `        ``ans ``+``=` `jumpWays(arr, n, pos ``+` `i, dp)` `    ``dp[pos] ``=` `ans` `    ``return` `ans`     `arr ``=` `[``1``, ``3``, ``5``, ``8``, ``9``, ``1``, ``0``, ``7``, ``6``, ``8``, ``9``]` `n ``=` `len``(arr)` `dp ``=` `[``-``1``]``*``n` `print``(jumpWays(arr, n, ``0``, dp))`

## Javascript

 `// Define a function to find the number of ways to reach the end of an array` `function` `jumpWays(arr, n, pos, dp) {` `    ``// Base case` `    ``if``(pos == n-1) {` `        ``return` `1;` `    ``}` `    ``if``(pos >= n) {` `        ``return` `0;` `    ``}` `    ``// Memoization` `    ``if``(dp[pos] != -1) {` `        ``return` `dp[pos];` `    ``}` `    ``let ans = 0;` `    ``for``(let i = 1; i <= arr[pos]; i++) {` `        ``ans += jumpWays(arr, n, pos + i, dp);` `    ``}` `    ``dp[pos] = ans;` `    ``return` `ans;` `}`   `// Driver Code` `let arr = [1, 3, 5, 8, 9, 1, 0, 7, 6, 8, 9];` `let n = arr.length;` `let dp = ``new` `Array(n).fill(-1);` `console.log(jumpWays(arr, n, 0, dp));`

## C#

 `using` `System;`   `class` `Gfg {` `    ``static` `int` `jumpWays(``int``[] arr, ``int` `n, ``int` `pos, ``int``[] dp) {` `        ``// Base case` `        ``if``(pos == n-1) {` `            ``return` `1;` `        ``}` `        ``if``(pos >= n) {` `            ``return` `0;` `        ``}` `        ``// Memoization` `        ``if``(dp[pos] != -1) {` `            ``return` `dp[pos];` `        ``}` `        ``int` `ans = 0;` `        ``for``(``int` `i = 1; i <= arr[pos]; i++) {` `            ``ans += jumpWays(arr, n, pos + i, dp);` `        ``}` `        ``dp[pos] = ans;` `        ``return` `ans;` `    ``}`   `    ``static` `void` `Main() {` `        ``int``[] arr = {1, 3, 5, 8, 9, 1, 0, 7, 6, 8, 9};` `        ``int` `n = arr.Length;` `        ``int``[] dp = ``new` `int``[n];` `        ``Array.Fill(dp, -1);` `        ``Console.WriteLine(jumpWays(arr, n, 0, dp));` `    ``}` `}`

Output

`52`

Time Complexity:
The time complexity of the algorithm using recursion and memoization is O(n^2), where n is the number of cells in the given array. This is because we need to compute the number of ways to reach the end from each cell, and there are n cells in the array. Each computation takes O(n) time, as there are at most n valid jumps from each cell.

Space Complexity:
The space complexity of the algorithm using recursion and memoization is O(n), where n is the number of cells in the given array. This is because we need to create a memoization table of size n+1 to store the results of the recursive function calls. Each cell in the memoization table stores an integer value, so the total space required is O(n).

My Personal Notes arrow_drop_up