Open In App

Maximize the sum of elements arr[i] selected by jumping index by value i

Last Updated : 17 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:   
 

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to to find the maximum
// score from the given array.
void findMaximumScore(int arr[], int n)
{
    // Initialize dp.
    int dp[n + 1] = { 0 };
    dp[n - 1] = arr[n - 1];
 
    // Store the max sum
    int ans = 0;
 
    // Iterating backwards from n-2 to 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];
        }
    }
 
    // Finding the maximum
    // score present in the dp.
    for (int i = 0; i < n; i++) {
        ans = max(dp[i], ans);
    }
    cout << ans << endl;
}
 
// Driver Code
int main()
{
    int n = 5;
    int arr[] = { 7, 3, 1, 2, 3 };
    findMaximumScore(arr, n);
    return 0;
}


Java




// Java program for the above approach
public class GFG {
     
    // Function to to find the maximum
    // score from the given array.
    static void findMaximumScore(int arr[], int n)
    {
       
        // Initialize dp.
        int dp[] = new int[n + 1];
        dp[n - 1] = arr[n - 1];
     
        // Store the max sum
        int ans = 0;
     
        // Iterating backwards from n-2 to 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];
            }
        }
     
        // Finding the maximum
        // score present in the dp.
        for (int i = 0; i < n; i++) {
            ans = Math.max(dp[i], ans);
        }
        System.out.println(ans);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 5;
        int arr[] = { 7, 3, 1, 2, 3 };
        findMaximumScore(arr, n);
    }
 
}
 
// This code is contributed by AnkThon


Python3




# Python Program to implement
# the above approach
 
# Function to to find the maximum
# score from the given array.
def findMaximumScore(arr, n):
 
    # Initialize dp.
    dp = [0] * (n + 1)
    dp[n - 1] = arr[n - 1]
 
    # Store the max sum
    ans = 0
 
    # Iterating backwards from n-2 to 0.
    for i in range(n-2, -1, -1):
        dp[i] = arr[i]
        j = i + arr[i]
        if (j < n):
            dp[i] += dp[j]
         
    # Finding the maximum
    # score present in the dp.
    for i in range(n):
        ans = max(dp[i], ans)
 
    print(ans)
 
# Driver Code
n = 5
arr = [7, 3, 1, 2, 3]
findMaximumScore(arr, n)
 
# This code is contributed by Saurabh Jaiswal


C#




// C# program for the above approach
 
using System;
 
public class GFG {
     
    // Function to to find the maximum
    // score from the given array.
    static void findMaximumScore(int []arr, int n)
    {
     
        // Initialize dp.
        int []dp = new int[n + 1];
        dp[n - 1] = arr[n - 1];
     
        // Store the max sum
        int ans = 0;
     
        // Iterating backwards from n-2 to 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];
            }
        }
     
        // Finding the maximum
        // score present in the dp.
        for (int i = 0; i < n; i++) {
            ans = Math.Max(dp[i], ans);
        }
        Console.WriteLine(ans);
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int n = 5;
        int []arr = { 7, 3, 1, 2, 3 };
        findMaximumScore(arr, n);
    }
 
}
 
// This code is contributed by AnkThon


Javascript




<script>
       // JavaScript Program to implement
       // the above approach
 
       // Function to to find the maximum
       // score from the given array.
       function findMaximumScore(arr, n)
       {
        
           // Initialize dp.
           let dp = new Array(n + 1).fill(0)
           dp[n - 1] = arr[n - 1];
 
           // Store the max sum
           let ans = 0;
 
           // Iterating backwards from n-2 to 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];
               }
           }
 
           // Finding the maximum
           // score present in the dp.
           for (let i = 0; i < n; i++) {
               ans = Math.max(dp[i], ans);
           }
           document.write(ans + '<br>');
       }
 
       // Driver Code
       let n = 5;
       let arr = [7, 3, 1, 2, 3];
       findMaximumScore(arr, n);
 
   // This code is contributed by Potta Lokesh
   </script>


Output

7

Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads