Given an array arr[], the task is to calculate the maximum sum of elements from the array such that, if an ith element is picked, then arr[i] is added to the sum and i jumps are taken ahead from the current index.
Examples:
Input: N = 5, arr[] = {7, 3, 1, 2, 3}
Output: 7
Explanation:
- Take the first element 7 to our score, then we move 7 index ahead and goes out of the array so the final answer is 7.
- Start from 2nd element 3 and add it to the score, then move 3 indices ahead and land on the last index of the array and add it to our score, so the final score will be 3+3=6.
- Start from 3rd element 1 and add to our score then move 1 index ahead and add 2 to our score, so the final score is =1+2=3.
- Start from 4th element 2 and add it to the score and move 2 indices ahead as we reach out of the array, the final score is 2.
- Start from 5th element 3 and add it to our score and our final score will be 3.
So from all the cases, the maximum score is 7.
Input: N = 2, arr[] = {3, 1}
Output: 3
Approach: The task can be solved using Dynamic Programming. Take a dp container that will store the maximum score from that particular index to the end of the array and after finding out the maximum score for all the indexes print the maximum score from them.
Follow the below steps to solve the problem:
- Create a Dp container of the same size of the array and ans variable.
- Now assign dp[n-1] as arr[n-1] because the maximum sum from that index is a[n-1].
- Iterate the loop backward from n-2 to 0.
- For each index, add arr[i] to the dp and check
- if i+arr[i] is less than size of array
- if yes, add dp[i+arr[i]] to dp[i].
- Iterate the dp array and find the maximum of the dp and store it in our ans variable.
- Finally, print ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMaximumScore( int arr[], int n)
{
int dp[n + 1] = { 0 };
dp[n - 1] = arr[n - 1];
int ans = 0;
for ( int i = n - 2; i >= 0; i--) {
dp[i] = arr[i];
int j = i + arr[i];
if (j < n) {
dp[i] += dp[j];
}
}
for ( int i = 0; i < n; i++) {
ans = max(dp[i], ans);
}
cout << ans << endl;
}
int main()
{
int n = 5;
int arr[] = { 7, 3, 1, 2, 3 };
findMaximumScore(arr, n);
return 0;
}
|
Java
public class GFG {
static void findMaximumScore( int arr[], int n)
{
int dp[] = new int [n + 1 ];
dp[n - 1 ] = arr[n - 1 ];
int ans = 0 ;
for ( int i = n - 2 ; i >= 0 ; i--) {
dp[i] = arr[i];
int j = i + arr[i];
if (j < n) {
dp[i] += dp[j];
}
}
for ( int i = 0 ; i < n; i++) {
ans = Math.max(dp[i], ans);
}
System.out.println(ans);
}
public static void main (String[] args)
{
int n = 5 ;
int arr[] = { 7 , 3 , 1 , 2 , 3 };
findMaximumScore(arr, n);
}
}
|
Python3
def findMaximumScore(arr, n):
dp = [ 0 ] * (n + 1 )
dp[n - 1 ] = arr[n - 1 ]
ans = 0
for i in range (n - 2 , - 1 , - 1 ):
dp[i] = arr[i]
j = i + arr[i]
if (j < n):
dp[i] + = dp[j]
for i in range (n):
ans = max (dp[i], ans)
print (ans)
n = 5
arr = [ 7 , 3 , 1 , 2 , 3 ]
findMaximumScore(arr, n)
|
C#
using System;
public class GFG {
static void findMaximumScore( int []arr, int n)
{
int []dp = new int [n + 1];
dp[n - 1] = arr[n - 1];
int ans = 0;
for ( int i = n - 2; i >= 0; i--) {
dp[i] = arr[i];
int j = i + arr[i];
if (j < n) {
dp[i] += dp[j];
}
}
for ( int i = 0; i < n; i++) {
ans = Math.Max(dp[i], ans);
}
Console.WriteLine(ans);
}
public static void Main ( string [] args)
{
int n = 5;
int []arr = { 7, 3, 1, 2, 3 };
findMaximumScore(arr, n);
}
}
|
Javascript
<script>
function findMaximumScore(arr, n)
{
let dp = new Array(n + 1).fill(0)
dp[n - 1] = arr[n - 1];
let ans = 0;
for (let i = n - 2; i >= 0; i--) {
dp[i] = arr[i];
let j = i + arr[i];
if (j < n) {
dp[i] += dp[j];
}
}
for (let i = 0; i < n; i++) {
ans = Math.max(dp[i], ans);
}
document.write(ans + '<br>' );
}
let n = 5;
let arr = [7, 3, 1, 2, 3];
findMaximumScore(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)