Count number of ways to jump to reach end in C++

C++Server Side ProgrammingProgramming

Given an array of positive numbers. Each element represents the maximum number of jumps that can be made from that index to reach the end of the array. The goal is to find the number of jumps that can be made from that element to reach the end. If arr[] is [ 1,2,3 ] then for 1 jumps can be 1, for 2 jumps can be 1 or 2 and for 3 jumps can be made 1, 2 or 3.

For Example

Input

arr[] = {1,2,3}

Output

Count of number of ways to jump to reach end are: 1 1 0

Explanation

For 1 we have jumps : 1,
For 2 we have jumps 1 or 2, to reach the end we need just one jump of 1.
For 3 we have jumps 1,2 or 3, as at its end we need no jumps.

Input

arr[] = {4,3,6,2}

Output

Count of number of ways to jump to reach end are: 4 2 1 0

Explanation

For 4 we have jumps : 1, 2, 3 or 4. To reach the end we need only 1,2 or
3. Ways will be 4−3−6−2, 4−6−2, 4−2, 4−3−2. Total 4. For 3 we have jumps 1, 2 or 3 to reach the end we need both. Ways will be 3−6−2, 3−2. Total 2. For 6 we have jumps 1 to 5, to reach the end we need 1. Ways will be 6−2. Total 1.
For 2 we have jumps 1or 2, as at its end we need no jumps.

Approach used in the below program is as follows

In this approach for each element arr[i] we will add counts of ways to reach the end of the array for all elements that are ahead arr[i] and are reachable from the current element. Add 1 to this count for ways for arr[i] if we can directly reach to end from arr[i] in a single jump.

  • Take an integer array arr[].

  • Function reach_end(int arr[], int size) takes an array and returns the count of the number of ways to jump to reach end.

  • Take array arr_2[] to store ways to reach the end from each element of arr[].

  • Initialize whole arr_2[] with 0 using memset(arr_2, 0, sizeof(arr_2)).

  • Traverse arr[] using a for loop from i=size-2 to i=0, as the last element will not be considered.

  • Take temp = size − i − 1. If temp>=arr[i] then we can directly reach the end using one jump. Increment ways count for arr[i] using arr_2[i]++.

  • Now for all other elements that can reach to end and to which we can reach from arr[i], add counts of ways to arr_2[i].

  • For above traverse using a for loop from j=i+1 to j<size−1 and j<=arr[i]+i. If any arr[j] is not −1, add it to arr_2[i].

  • If still arr_2[i] is 0 then set it to −1, which means it cannot reach to end.

  • At the end of all loops we will have arr_2[] having ways to reach end for each element of arr[].

  • Print arr_2 using for loop as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void reach_end(int arr[], int size){
   int arr_2[size];
   memset(arr_2, 0, sizeof(arr_2));
   for (int i = size−2; i >= 0; i−−){
      int temp = size − i − 1;
      if (arr[i] >= temp){
         arr_2[i]++;
      }
      for (int j = i+1; j < size−1 && j <= arr[i] + i; j++){
         if (arr_2[j] != −1){
            arr_2[i] = arr_2[i] + arr_2[j];
         }
      }
      if(arr_2[i] == 0){
         arr_2[i] = −1;
      }
   }
   cout<<"Count of number of ways to jump to reach end are: ";
   for (int i=0; i < size; i++){
      cout<<arr_2[i] << " ";
   }
}
int main(){
   int arr[] = {2, 3, 7, 1, 8, 9};
   int size = sizeof(arr) / sizeof(arr[0]);
   reach_end(arr, size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of number of ways to jump to reach end are: 8 5 3 1 1 0
raja
Published on 05-Jan-2021 06:46:15
Advertisements