Maximum sum of non-overlapping subarrays of length atmost K
Last Updated :
19 Apr, 2023
Given an integer array ‘arr’ of length N and an integer ‘k’, select some non-overlapping subarrays such that each sub-array if of length at most ‘k’, no two sub-arrays are adjacent and sum of all the elements of the selected sub-arrays are maximum.
Examples:
Input : arr[] = {-1, 2, -3, 4, 5}, k = 2
Output : 11
Sub-arrays that maximizes sum will be {{2}, {4, 5}}.
Thus, the answer will be 2+4+5 = 11.
Input :arr[] = {1, 1, 1, 1, 1}, k = 1
Output : 3
Naive Approach : A simple way is to generate all possible subsets of elements satisfying above conditions recursively and find the subset with maximum sum.
Time Complexity : O(2N)
Efficient Approach: A better approach is to use dynamic programming.
Let’s suppose we are at an index ‘i’.
Let dp[i] be defined as the maximum sum of elements of all possible subsets of sub-array {i, n-1} satisfying above conditions.
We will have ‘K+1’ possible choices i.e.
- Reject ‘i’ and solve for ‘i+1’.
- Select sub-array {i} and solve for ‘i+2’
- Select sub-array {i, i+1} and solve for ‘i+3’
Thus, recurrence relation will be:
dp[i] = max(dp[i+1], arr[i]+dp[i+2], arr[i]+arr[i+1]+dp[i+3],
...arr[i]+arr[i+1]+arr[i+2]...+arr[i+k-1] + dp[i+k+1])
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define maxLen 10
using namespace std;
int dp[maxLen];
bool visit[maxLen];
int maxSum( int arr[], int i, int n, int k)
{
if (i >= n)
return 0;
if (visit[i])
return dp[i];
visit[i] = 1;
int tot = 0;
dp[i] = maxSum(arr, i + 1, n, k);
for ( int j = i; j < i + k and j < n; j++) {
tot += arr[j];
dp[i] = max(dp[i], tot +
maxSum(arr, j + 2, n, k));
}
return dp[i];
}
int main()
{
int arr[] = { -1, 2, -3, 4, 5 };
int k = 2;
int n = sizeof (arr) / sizeof ( int );
cout << maxSum(arr, 0, n, k);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int maxLen = 10 ;
static int dp[] = new int [maxLen];
static boolean []visit = new boolean [maxLen];
static int maxSum( int arr[], int i,
int n, int k)
{
if (i >= n)
return 0 ;
if (visit[i])
return dp[i];
visit[i] = true ;
int tot = 0 ;
dp[i] = maxSum(arr, i + 1 , n, k);
for ( int j = i; j < (i + k) &&
(j < n); j++)
{
tot += arr[j];
dp[i] = Math.max(dp[i], tot +
maxSum(arr, j + 2 , n, k));
}
return dp[i];
}
public static void main (String[] args)
{
int arr[] = { - 1 , 2 , - 3 , 4 , 5 };
int k = 2 ;
int n = arr.length;
System.out.println(maxSum(arr, 0 , n, k));
}
}
|
Python3
maxLen = 10
dp = [ 0 ] * maxLen;
visit = [ 0 ] * maxLen;
def maxSum(arr, i, n, k) :
if (i > = n) :
return 0 ;
if (visit[i]) :
return dp[i];
visit[i] = 1 ;
tot = 0 ;
dp[i] = maxSum(arr, i + 1 , n, k);
j = i
while (j < i + k and j < n) :
tot + = arr[j];
dp[i] = max (dp[i], tot +
maxSum(arr, j + 2 , n, k));
j + = 1
return dp[i];
if __name__ = = "__main__" :
arr = [ - 1 , 2 , - 3 , 4 , 5 ];
k = 2 ;
n = len (arr);
print (maxSum(arr, 0 , n, k));
|
C#
using System;
class GFG
{
static int maxLen = 10;
static int []dp = new int [maxLen];
static bool []visit = new bool [maxLen];
static int maxSum( int []arr, int i,
int n, int k)
{
if (i >= n)
return 0;
if (visit[i])
return dp[i];
visit[i] = true ;
int tot = 0;
dp[i] = maxSum(arr, i + 1, n, k);
for ( int j = i; j < (i + k) &&
(j < n); j++)
{
tot += arr[j];
dp[i] = Math.Max(dp[i], tot +
maxSum(arr, j + 2, n, k));
}
return dp[i];
}
static public void Main ()
{
int []arr = { -1, 2, -3, 4, 5 };
int k = 2;
int n = arr.Length;
Console.WriteLine (maxSum(arr, 0, n, k));
}
}
|
Javascript
<script>
let maxLen = 10;
let dp = new Array(maxLen);
let visit = new Array(maxLen);
function maxSum(arr, i, n, k)
{
if (i >= n)
return 0;
if (visit[i])
return dp[i];
visit[i] = true ;
let tot = 0;
dp[i] = maxSum(arr, i + 1, n, k);
for (let j = i; j < (i + k) &&
(j < n); j++)
{
tot += arr[j];
dp[i] = Math.max(dp[i], tot +
maxSum(arr, j + 2, n, k));
}
return dp[i];
}
let arr = [ -1, 2, -3, 4, 5 ];
let k = 2;
let n = arr.length;
document.write(maxSum(arr, 0, n, k));
</script>
|
Time Complexity: O(n*k)
Space complexity: O(n)
Efficient approach : Using DP Tabulation method ( Iterative approach )
The approach to solve this problem is same but DP tabulation(bottom-up) method is better then Dp + memoization(top-down) because memoization method needs extra stack space of recursion calls.
Steps to solve this problem :
- Create a 1D DP of size n to store the solution of the subproblems.
- Initialize the DP with 0.
- Now Iterate over subproblems to get the value of current problem form previous computation of subproblems stored in DP
- Create a variable res and to store the final result and update it by iterating through Dp.
- Return the final solution stored in res
Implementation :
C++
#include <bits/stdc++.h>
#define maxLen 10
using namespace std;
int maxSum( int arr[], int n, int k)
{
int dp[n];
memset (dp, 0, sizeof (dp));
for ( int i = 0; i < n; i++) {
int tot = 0;
for ( int j = i - k; j < i; j++) {
if (j >= 0)
tot = max(tot, dp[j]);
}
dp[i] = tot + arr[i];
}
int res = 0;
for ( int i = 0; i < n; i++)
res = max(res, dp[i]);
return res;
}
int main()
{
int arr[] = { -1, 2, -3, 4, 5 };
int k = 2;
int n = sizeof (arr) / sizeof ( int );
cout << maxSum(arr, n, k);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static int maxSum( int arr[], int n, int k)
{
int dp[] = new int [n];
Arrays.fill(dp, 0 );
for ( int i = 0 ; i < n; i++) {
int tot = 0 ;
for ( int j = i - k; j < i; j++) {
if (j >= 0 )
tot = Math.max(tot, dp[j]);
}
dp[i] = tot + arr[i];
}
int res = 0 ;
for ( int i = 0 ; i < n; i++)
res = Math.max(res, dp[i]);
return res;
}
public static void main(String[] args)
{
int arr[] = { - 1 , 2 , - 3 , 4 , 5 };
int k = 2 ;
int n = arr.length;
System.out.println(maxSum(arr, n, k));
}
}
|
Python3
def maxSum(arr, n, k):
dp = [ 0 ] * n
for i in range (n):
tot = 0
for j in range (i - k, i):
if j > = 0 :
tot = max (tot, dp[j])
dp[i] = tot + arr[i]
res = 0
for i in range (n):
res = max (res, dp[i])
return res
arr = [ - 1 , 2 , - 3 , 4 , 5 ]
k = 2
n = len (arr)
print (maxSum(arr, n, k))
|
C#
using System;
class MaxSum
{
static int ComputeMaxSum( int [] arr, int n, int k)
{
int [] dp = new int [n];
for ( int i = 0; i < n; i++)
{
int tot = 0;
for ( int j = i - k; j < i; j++)
{
if (j >= 0)
{
tot = Math.Max(tot, dp[j]);
}
}
dp[i] = tot + arr[i];
}
int res = 0;
for ( int i = 0; i < n; i++)
{
res = Math.Max(res, dp[i]);
}
return res;
}
static void Main()
{
int [] arr = {-1, 2, -3, 4, 5};
int k = 2;
int n = arr.Length;
Console.WriteLine(ComputeMaxSum(arr, n, k));
}
}
|
Javascript
function maxSum(arr, n, k)
{
let dp = new Array(n);
dp.fill(0);
for (let i = 0; i < n; i++) {
let tot = 0;
for (let j = i - k; j < i; j++) {
if (j >= 0)
tot = Math.max(tot, dp[j]);
}
dp[i] = tot + arr[i];
}
let res = 0;
for (let i = 0; i < n; i++)
res = Math.max(res, dp[i]);
return res;
}
function main()
{
let arr = [ -1, 2, -3, 4, 5 ];
let k = 2;
let n = arr.length;
console.log(maxSum(arr, n, k));
}
main();
|
Output
11
Time Complexity: O(n*k)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...