Maximize sum possible from an array by jumps of length i + K * arr[i] from any ith index
Last Updated :
07 Apr, 2023
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 = 2
Output: 4
Explanation:
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 = 3
Output: 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int max_sum(vector< int >& arr, int K) {
int n = arr.size();
int max_sum = INT_MIN;
for ( int i = 0; i < n; i++) {
int cur_sum = 0;
int j = i;
while (j < n) {
cur_sum += arr[j];
j = j + i + K*arr[j];
}
max_sum = max(max_sum, cur_sum);
}
return max_sum;
}
int main() {
vector< int > arr = {2, 1, 3, 1, 2};
int K = 3;
cout << max_sum(arr, K) << endl;
return 0;
}
|
C#
using System;
public class Program {
public static int MaxSum( int [] arr, int K)
{
int n = arr.Length;
int max_sum = int .MinValue;
for ( int i = 0; i < n; i++) {
int cur_sum = 0;
int j = i;
while (j < n) {
cur_sum += arr[j];
j = j + i + K * arr[j];
}
max_sum = Math.Max(max_sum, cur_sum);
}
return max_sum;
}
public static void Main()
{
int [] arr = { 2, 1, 3, 1, 2 };
int K = 3;
Console.WriteLine(MaxSum(arr, K));
}
}
|
Python
import sys
def max_sum(arr, K):
n = len (arr)
max_sum = - sys.maxsize - 1
for i in range (n):
cur_sum = 0
j = i
while j < n:
cur_sum + = arr[j]
j = j + i + K * arr[j]
max_sum = max (max_sum, cur_sum)
return max_sum
if __name__ = = "__main__" :
arr = [ 2 , 1 , 3 , 1 , 2 ]
K = 3
print (max_sum(arr, K))
|
Java
import java.io.*;
class GFG {
public static int maxsum( int [] arr, int K)
{
int n = arr.length;
int max_sum = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++) {
int cur_sum = 0 ;
int j = i;
while (j < n) {
cur_sum += arr[j];
j = j + i + K * arr[j];
}
max_sum = Math.max(max_sum, cur_sum);
}
return max_sum;
}
public static void main(String[] args)
{
int [] arr = { 2 , 1 , 3 , 1 , 2 };
int K = 3 ;
System.out.print(maxsum(arr, K));
}
}
|
Javascript
function max_sum(arr, K)
{
let n = arr.length;
let max_sum = Number.MIN_VALUE;
for (let i = 0; i < n; i++)
{
let cur_sum = 0;
let j = i;
while (j < n) {
cur_sum += arr[j];
j = j + i + K * arr[j];
}
max_sum = Math.max(max_sum, cur_sum);
}
return max_sum;
}
let arr = { 2, 1, 3, 1, 2 };
let K = 3;
console.log(maxsum(arr, K));
|
Output:
3
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].
- 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++
#include <bits/stdc++.h>
using namespace std;
void maxSum( int arr[], int N, int K)
{
int dp[N + 2] = { 0 };
int maxval = 0;
for ( int i = N - 1; i >= 0; i--) {
if ((i + K * arr[i]) >= N) {
dp[i] = arr[i];
}
else {
dp[i] = dp[i + K * arr[i]] + arr[i];
}
maxval = max(maxval, dp[i]);
}
cout << maxval;
}
int 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
import java.util.*;
class GFG
{
static void maxSum( int arr[], int N, int K)
{
int [] dp = new int [N + 2 ];
Arrays.fill(dp, 0 );
int maxval = 0 ;
for ( int i = N - 1 ; i >= 0 ; i--)
{
if ((i + K * arr[i]) >= N)
{
dp[i] = arr[i];
}
else {
dp[i] = dp[i + K * arr[i]] + arr[i];
}
maxval = Math.max(maxval, dp[i]);
}
System.out.print(maxval);
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 3 , 1 , 2 };
int N = arr.length;
int K = 3 ;
maxSum(arr, N, K);
}
}
|
Python3
def maxSum(arr, N, K):
dp = [ 0 for i in range (N + 2 )]
maxval = 0
i = N - 1
while (i > = 0 ):
if ((i + K * arr[i]) > = N):
dp[i] = arr[i]
else :
dp[i] = dp[i + K * arr[i]] + arr[i]
maxval = max (maxval, dp[i])
i - = 1
print (maxval)
if __name__ = = '__main__' :
arr = [ 2 , 1 , 3 , 1 , 2 ]
N = len (arr)
K = 3
maxSum(arr, N, K)
|
C#
using System;
class GFG
{
static void maxSum( int [] arr, int N, int K)
{
int [] dp = new int [N + 2];
for ( int i = 0; i< N+2; i++)
{
dp[i] = 0;
}
int maxval = 0;
for ( int i = N - 1; i >= 0; i--)
{
if ((i + K * arr[i]) >= N)
{
dp[i] = arr[i];
}
else {
dp[i] = dp[i + K * arr[i]] + arr[i];
}
maxval = Math.Max(maxval, dp[i]);
}
Console.WriteLine(maxval);
}
static public void Main()
{
int [] arr = { 2, 1, 3, 1, 2 };
int N = arr.Length;
int K = 3;
maxSum(arr, N, K);
}
}
|
Javascript
<script>
function maxSum(arr, N, K)
{
let dp = [];
for (let i = 0; i < N + 2; i++)
{
dp[i] = 0;
}
let maxval = 0;
for (let i = N - 1; i >= 0; i--)
{
if ((i + K * arr[i]) >= N)
{
dp[i] = arr[i];
}
else {
dp[i] = dp[i + K * arr[i]] + arr[i];
}
maxval = Math.max(maxval, dp[i]);
}
document.write(maxval);
}
let arr = [ 2, 1, 3, 1, 2 ];
let N = arr.length;
let K = 3;
maxSum(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...