Counts Path in an Array
Last Updated :
23 Jun, 2022
Given an array A consisting of positive integer, of size N. If the element in the array at index i is K then you can jump between index ranges (i + 1) to (i + K).
The task is to find the number of possible ways to reach the end with module 109 + 7.
The starting position is considered as index 0.
Examples:
Input: A = {5, 3, 1, 4, 3}
Output: 6
Input: A = {2, 3, 1, 1, 2}
Output: 4
Naive Approach: We can form a recursive structure to solve the problem.
Let F[i] denotes the number of paths starting at index i, at every index i if the element A[i] is K then the total number of ways the jump can be performed is:
F(i) = F(i+1) + F(i+2) +...+ F(i+k), where i + k <= n,
where F(n) = 1
By using this recursive formula we can solve the problem:
C++
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int ways( int i, int arr[], int n)
{
if (i == n - 1)
return 1;
int sum = 0;
for ( int j = 1;
j + i < n && j <= arr[i];
j++) {
sum += (ways(i + j,
arr, n))
% mod;
sum %= mod;
}
return sum % mod;
}
int main()
{
int arr[] = { 5, 3, 1, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << ways(0, arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int mod = 1000000000 ;
static int ways( int i,
int arr[], int n)
{
if (i == n - 1 )
return 1 ;
int sum = 0 ;
for ( int j = 1 ; j + i < n &&
j <= arr[i]; j++)
{
sum += (ways(i + j,
arr, n)) % mod;
sum %= mod;
}
return sum % mod;
}
public static void main (String[] args)
{
int arr[] = { 5 , 3 , 1 , 4 , 3 };
int n = arr.length;
System.out.println (ways( 0 , arr, n));
}
}
|
Python3
mod = 1e9 + 7 ;
def ways(i, arr, n):
if (i = = n - 1 ):
return 1 ;
sum = 0 ;
for j in range ( 1 , arr[i] + 1 ):
if (i + j < n):
sum + = (ways(i + j, arr, n)) % mod;
sum % = mod;
return int ( sum % mod);
if __name__ = = '__main__' :
arr = [ 5 , 3 , 1 , 4 , 3 ];
n = len (arr);
print (ways( 0 , arr, n));
|
C#
using System;
class GFG
{
static int mod = 1000000000;
static int ways( int i,
int []arr, int n)
{
if (i == n - 1)
return 1;
int sum = 0;
for ( int j = 1; j + i < n &&
j <= arr[i]; j++)
{
sum += (ways(i + j,
arr, n)) % mod;
sum %= mod;
}
return sum % mod;
}
public static void Main (String[] args)
{
int []arr = { 5, 3, 1, 4, 3 };
int n = arr.Length;
Console.WriteLine(ways(0, arr, n));
}
}
|
Javascript
<script>
let mod = 1000000000;
function ways(i, arr, n)
{
if (i == n - 1)
return 1;
let sum = 0;
for (let j = 1; j + i < n && j <= arr[i]; j++)
{
sum += (ways(i + j, arr, n)) % mod;
sum %= mod;
}
return sum % mod;
}
let arr = [ 5, 3, 1, 4, 3 ];
let n = arr.length;
document.write(ways(0, arr, n));
</script>
|
Efficient Approach: In the previous approach, there are some calculations that are being done more than once. It will be better to store these values in a dp array and dp[i] will store the number of paths starting at index i and ending at the end of the array.
Hence dp[0] will be the solution to the problem.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int ways( int arr[], int n)
{
int dp[n + 1];
dp[n - 1] = 1;
for ( int i = n - 2; i >= 0; i--) {
dp[i] = 0;
for ( int j = 1; ((j + i) < n
&& j <= arr[i]);
j++) {
dp[i] += dp[i + j];
dp[i] %= mod;
}
}
return dp[0] % mod;
}
int main()
{
int arr[] = { 5, 3, 1, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << ways(arr, n) % mod << endl;
return 0;
}
|
Java
class GFG
{
static final int mod = ( int )(1e9 + 7 );
static int ways( int arr[], int n)
{
int dp[] = new int [n + 1 ];
dp[n - 1 ] = 1 ;
for ( int i = n - 2 ; i >= 0 ; i--)
{
dp[i] = 0 ;
for ( int j = 1 ; ((j + i) < n &&
j <= arr[i]); j++)
{
dp[i] += dp[i + j];
dp[i] %= mod;
}
}
return dp[ 0 ] % mod;
}
public static void main (String[] args)
{
int arr[] = { 5 , 3 , 1 , 4 , 3 };
int n = arr.length;
System.out.println(ways(arr, n) % mod);
}
}
|
Python3
mod = 10 * * 9 + 7
def ways(arr, n):
dp = [ 0 ] * (n + 1 )
dp[n - 1 ] = 1
for i in range (n - 2 , - 1 , - 1 ):
dp[i] = 0
j = 1
while ((j + i) < n and j < = arr[i]):
dp[i] + = dp[i + j]
dp[i] % = mod
j + = 1
return dp[ 0 ] % mod
arr = [ 5 , 3 , 1 , 4 , 3 ]
n = len (arr)
print (ways(arr, n) % mod)
|
C#
using System;
class GFG
{
static readonly int mod = ( int )(1e9 + 7);
static int ways( int []arr, int n)
{
int []dp = new int [n + 1];
dp[n - 1] = 1;
for ( int i = n - 2; i >= 0; i--)
{
dp[i] = 0;
for ( int j = 1; ((j + i) < n &&
j <= arr[i]); j++)
{
dp[i] += dp[i + j];
dp[i] %= mod;
}
}
return dp[0] % mod;
}
public static void Main (String[] args)
{
int []arr = { 5, 3, 1, 4, 3 };
int n = arr.Length;
Console.WriteLine(ways(arr, n) % mod);
}
}
|
Javascript
<script>
let mod = (1e9 + 7);
function ways(arr, n)
{
let dp = new Array(n + 1);
dp.fill(0);
dp[n - 1] = 1;
for (let i = n - 2; i >= 0; i--)
{
dp[i] = 0;
for (let j = 1; ((j + i)
< n && j <= arr[i]); j++)
{
dp[i] += dp[i + j];
dp[i] %= mod;
}
}
return dp[0] % mod;
}
let arr = [ 5, 3, 1, 4, 3 ];
let n = arr.length;
document.write(ways(arr, n) % mod);
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...