Given an integer N and an array arr[ ] of size N, the task is to find the maximum jumps to reach the end of the array given the constraint that from index i can make arr[i] jumps and reach the position i+arr[i] .
Examples:
Input: N = 5, arr[] = {2, 3, 5, 7, 9}Output: 12 Explanation: At index 0 make 2 jumps and move to index 2 and make 5 jumps after that to reach index 7 which is out of the array so total number of jumps is (2+5)=7. At index 1 make 3+9= 12 jumps At index 2 make 5 jumps At index 3 make 7 jumps At index 4 make 9 jumps
Input: arr[] ={2, 2, 1, 2, 3, 3}Output: 8
Approach: The idea is to use Dynamic programming to solve this problem. Follow the steps below to solve the problem.
Initialize an array dp of size N with 0. dp[i] stores the number of jumps needed to reach the end of the array from index i . Also, initialize an integer ans to 0. Traverse through the array from the end of the array using for loopAssign arr[i] to dp[i] since arr[i] is the smallest number of jumps from this index. Now initialize a variable say j and assign j = i+arr[i]. If the value of j is less than N , then add dp[j] to dp[i] . Update the value of ans as max (ans , dp[i] ) After completing the above steps print the value of ans.
C++
#include <bits/stdc++.h>
using namespace std;
void findMaxJumps(int arr[], int N)
{
int dp[N] = { 0 };
int ans = 0;
for (int i = N - 1; i >= 0; i--) {
dp[i] = arr[i];
int j = i + arr[i];
if (j < N) {
dp[i] = dp[i] + dp[j];
}
ans = max(ans, dp[i]);
}
cout << ans;
}
int main()
{
int arr[] = { 2, 3, 5, 7, 9 };
int N = sizeof(arr) / sizeof(arr[0]);
findMaxJumps(arr, N);
return 0;
}
Java
import java.io.*;
class GFG {
static void findMaxJumps(int arr[], int N)
{
int dp[] = new int [N];
int ans = 0;
for (int i = N - 1; i >= 0; i--) {
dp[i] = arr[i];
int j = i + arr[i];
if (j < N) {
dp[i] = dp[i] + dp[j];
}
ans = Math.max(ans, dp[i]);
}
System.out.println(ans);
}
public static void main (String[] args) {
int arr[] = { 2, 3, 5, 7, 9 };
int N = arr.length;
findMaxJumps(arr, N);
}
}
Python3
def findMaxJumps(arr, N):
dp = [0 for i in range(N)]
ans = 0
i = N - 1
while(i >= 0):
dp[i] = arr[i]
j = i + arr[i]
if (j < N):
dp[i] = dp[i] + dp[j]
ans = max(ans, dp[i])
i -= 1
print(ans)
if __name__ == '__main__':
arr = [2, 3, 5, 7, 9]
N = len(arr)
findMaxJumps(arr, N)
C#
using System;
class GFG {
static void findMaxJumps(int[] arr, int N)
{
int[] dp = new int[N];
int ans = 0;
for (int i = N - 1; i >= 0; i--) {
dp[i] = arr[i];
int j = i + arr[i];
if (j < N) {
dp[i] = dp[i] + dp[j];
}
ans = Math.Max(ans, dp[i]);
}
Console.Write(ans);
}
public static void Main(string[] args)
{
int[] arr = { 2, 3, 5, 7, 9 };
int N = arr.Length;
findMaxJumps(arr, N);
}
}
Javascript
<script>
function findMaxJumps(arr, N)
{
let dp = new Array(N).fill(0);
let ans = 0;
for (let i = N - 1; i >= 0; i--) {
dp[i] = arr[i];
let j = i + arr[i];
if (j < N) {
dp[i] = dp[i] + dp[j];
}
ans = Math.max(ans, dp[i]);
}
document.write(ans);
}
let arr = [2, 3, 5, 7, 9];
let N = arr.length;
findMaxJumps(arr, N);
</script>
Time Complexity: O(N)Auxiliary Space: O(N)
Last Updated :
22 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...