Maximize sum possible from an array by jumps of length i + K * arr[i] from any ith index - GeeksforGeeks

28 Apr, 2021
Given an array arr[] consisting of N positive integers and an integer K, the task is to find the maximum sum of array elements possible by jumps of (i + K*arr[i]) (< N) length from any ith index of the array.
Examples:
Input: arr[] = {1, 2, 1, 4}, K = 2Output: 4Explanation:Starting index i = 0, the traversal of indices of array is {0, 2}Hence, the answer is 4.
Input: arr[] = {2, 1, 3, 1, 2}, K = 3Output: 3
Naive Approach: The simplest approach is to traverse the given array for each possible starting index over the range [0, N – 1] and find the maximum of all the sum obtained by taking the jump of (i + K*arr[i]) from each possible index i. After all the traversal print the maximum sum obtained.Time Complexity: O(N2)Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using Dynamic Programming. The idea is to use an auxiliary array dp[] such that dp[i] stores the sum that can be obtained by choosing i as the starting index by using the following recurrence relation:
dp[i] = dp[i + K*arr[i]] + arr[i]
 Follow the steps below to solve the problem:
Initialize an auxiliary array dp[] of size N with {0}.
Iterate over the range [N – 1, 0] using the variable i and perform the following steps:If the value of (i + K*arr[i] ≥ N), then update the value of dp[i] as arr[i].Otherwise, update the value of dp[i] as dp[i + K*arr[i]] + arr[i].
If the value of (i + K*arr[i] ≥ N), then update the value of dp[i] as arr[i].
Otherwise, update the value of dp[i] as dp[i + K*arr[i]] + arr[i].
After completing the above steps, print the maximum value in the array dp[] as the resultant maximum sum.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the maximum sum// possible by jumps of length// i + K*arr[i] from any i-th indexvoid maxSum(int arr[], int N, int K){    // Initialize an array dp[]    int dp[N + 2] = { 0 };     // Stores the maximum sum    int maxval = 0;     // Iterate over the range [N-1, 0]    for (int i = N - 1; i >= 0; i--) {         // If length of the jump exceeds N        if ((i + K * arr[i]) >= N) {             // Set dp[i] as arr[i]            dp[i] = arr[i];        }         // Otherwise, update dp[i] as        // sum of dp[i + K * arr[i]] and arr[i]        else {            dp[i] = dp[i + K * arr[i]] + arr[i];        }         // Update the overall maximum sum        maxval = max(maxval, dp[i]);    }     // Print the answer    cout << maxval;} // Driver Codeint main(){    int arr[] = { 2, 1, 3, 1, 2 };    int N = sizeof(arr) / sizeof(arr[0]);    int K = 3;     maxSum(arr, N, K);     return 0;}
// Java program for the above approachimport java.util.*;class GFG{ // Function to find the maximum sum// possible by jumps of length// i + K*arr[i] from any i-th indexstatic void maxSum(int arr[], int N, int K){       // Initialize an array dp[]    int[] dp = new int[N + 2];    Arrays.fill(dp, 0);     // Stores the maximum sum    int maxval = 0;     // Iterate over the range [N-1, 0]    for (int i = N - 1; i >= 0; i--)    {         // If length of the jump exceeds N        if ((i + K * arr[i]) >= N)        {             // Set dp[i] as arr[i]            dp[i] = arr[i];        }         // Otherwise, update dp[i] as        // sum of dp[i + K * arr[i]] and arr[i]        else {            dp[i] = dp[i + K * arr[i]] + arr[i];        }         // Update the overall maximum sum        maxval = Math.max(maxval, dp[i]);    }     // Print the answer    System.out.print(maxval);}  // Driver Codepublic static void main(String[] args){    int arr[] = { 2, 1, 3, 1, 2 };    int N = arr.length;    int K = 3;    maxSum(arr, N, K);}} // This code is contributed by code_hunt.
# Python 3 program for the above approach # Function to find the maximum sum# possible by jumps of length# i + K*arr[i] from any i-th indexdef maxSum(arr, N, K):    # Initialize an array dp[]    dp = [0 for i in range(N+2)]     # Stores the maximum sum    maxval = 0     # Iterate over the range [N-1, 0]    i = N - 1    while(i >= 0):        # If length of the jump exceeds N        if ((i + K * arr[i]) >= N):            # Set dp[i] as arr[i]            dp[i] = arr[i]         # Otherwise, update dp[i] as        # sum of dp[i + K * arr[i]] and arr[i]        else:            dp[i] = dp[i + K * arr[i]] + arr[i]         # Update the overall maximum sum        maxval = max(maxval, dp[i])        i -= 1     # Print the answer    print(maxval) # Driver Codeif __name__ == '__main__':    arr =  [2, 1, 3, 1, 2]    N = len(arr)    K = 3    maxSum(arr, N, K)         # This code is contributed by SURENDRA_GANGWAR.
// C# program for the above approachusing System;class GFG{ // Function to find the maximum sum// possible by jumps of length// i + K*arr[i] from any i-th indexstatic void maxSum(int[] arr, int N, int K){       // Initialize an array dp[]    int[] dp = new int[N + 2];    for(int i = 0; i< N+2; i++)    {        dp[i] = 0;    }     // Stores the maximum sum    int maxval = 0;     // Iterate over the range [N-1, 0]    for (int i = N - 1; i >= 0; i--)    {         // If length of the jump exceeds N        if ((i + K * arr[i]) >= N)        {             // Set dp[i] as arr[i]            dp[i] = arr[i];        }         // Otherwise, update dp[i] as        // sum of dp[i + K * arr[i]] and arr[i]        else {            dp[i] = dp[i + K * arr[i]] + arr[i];        }         // Update the overall maximum sum        maxval = Math.Max(maxval, dp[i]);    }     // Print the answer    Console.WriteLine(maxval);} // Driver Codestatic public void Main(){    int[] arr = { 2, 1, 3, 1, 2 };    int N = arr.Length;    int K = 3;    maxSum(arr, N, K);}} // This code is contributed by susmitakundugoaldanga.
<script> // Javascript program for the above approach // Function to find the maximum sum// possible by jumps of length// i + K*arr[i] from any i-th indexfunction maxSum(arr, N, K){        // Initialize an array dp[]    let dp = [];    for(let i = 0; i < N + 2; i++)    {        dp[i] = 0;    }      // Stores the maximum sum    let maxval = 0;      // Iterate over the range [N-1, 0]    for (let i = N - 1; i >= 0; i--)    {          // If length of the jump exceeds N        if ((i + K * arr[i]) >= N)        {              // Set dp[i] as arr[i]            dp[i] = arr[i];        }          // Otherwise, update dp[i] as        // sum of dp[i + K * arr[i]] and arr[i]        else {            dp[i] = dp[i + K * arr[i]] + arr[i];        }          // Update the overall maximum sum        maxval = Math.max(maxval, dp[i]);    }      // Print the answer    document.write(maxval);} // Driver Code     let arr = [ 2, 1, 3, 1, 2 ];    let N = arr.length;    let K = 3;    maxSum(arr, N, K); </script>
  
3
 
 
Time Complexity: O(N)Auxiliary Space: O(N)
 
SURENDRA_GANGWAR
code_hunt
susmitakundugoaldanga
chinmoy1997pal
Technical Scripter 2020
Arrays
Dynamic Programming
Mathematical
Technical Scripter
Arrays
Dynamic Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
Count pairs with given sum
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
0-1 Knapsack Problem | DP-10
Program for Fibonacci numbers
Longest Common Subsequence | DP-4
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16