Max sum of M non-overlapping subarrays of size K
Given an array and two numbers M and K. We need to find the max sum of sums of M subarrays of size K (non-overlapping) in the array. (Order of array remains unchanged). K is the size of subarrays and M is the count of subarray. It may be assumed that size of array is more than m*k. If total array size is not multiple of k, then we can take partial last array.
Examples :
Input: N = 7, M = 3, K = 1
arr[] = {2, 10, 7, 18, 5, 33, 0};
Output: 61
Explanation: subsets are: 33, 18, 10 (3 subsets of size 1)
Input: N = 4, M = 2, K = 2
arr[] = {3, 2, 100, 1};
Output: 106
Explanation: subsets are: (3, 2), (100, 1) 2 subsets of size 2
Here we can see that we need to find M subarrays each of size K so,
- We create a presum array, which contains in each index sum of all elements from ‘index‘ to ‘index + K’ in the given array. And size of the sum array will be n+1-k.
- Now if we include the subarray of size k, then we can not include any of the elements of that subarray again in any other subarray as it will create overlapping subarrays. So we make recursive call by excluding the k elements of included subarray.
- if we exclude a subarray then we can use the next k-1 elements of that subarray in other subarrays so we will make recursive call by just excluding the first element of that subarray.
- At last return the max(included sum, excluded sum).
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void calculatePresumArray( int presum[],
int arr[], int n, int k)
{
for ( int i=0; i<k; i++)
presum[0] += arr[i];
for ( int i = 1; i <= n - k; i++)
presum[i] += presum[i-1] + arr[i+k-1] -
arr[i-1];
}
int maxSumMnonOverlappingSubarray( int presum[],
int m, int size, int k, int start)
{
if (m == 0)
return 0;
if (start > size - 1)
return 0;
int mx = 0;
int includeMax = presum[start] +
maxSumMnonOverlappingSubarray(presum,
m - 1, size, k, start + k);
int excludeMax =
maxSumMnonOverlappingSubarray(presum,
m, size, k, start + 1);
return max(includeMax, excludeMax);
}
int main()
{
int arr[] = { 2, 10, 7, 18, 5, 33, 0 };
int n = sizeof (arr)/ sizeof (arr[0]);
int m = 3, k = 1;
int presum[n + 1 - k] = { 0 };
calculatePresumArray(presum, arr, n, k);
cout << maxSumMnonOverlappingSubarray(presum,
m, n + 1 - k, k, 0);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void calculatePresumArray( int presum[],
int arr[],
int n, int k)
{
for ( int i = 0 ; i < k; i++)
presum[ 0 ] += arr[i];
for ( int i = 1 ; i <= n - k; i++)
presum[i] += presum[i - 1 ] + arr[i + k - 1 ] -
arr[i - 1 ];
}
static int maxSumMnonOverlappingSubarray( int presum[],
int m, int size,
int k, int start)
{
if (m == 0 )
return 0 ;
if (start > size - 1 )
return 0 ;
int mx = 0 ;
int includeMax = presum[start] +
maxSumMnonOverlappingSubarray(presum,
m - 1 , size, k, start + k);
int excludeMax =
maxSumMnonOverlappingSubarray(presum,
m, size, k, start + 1 );
return Math.max(includeMax, excludeMax);
}
public static void main (String[] args)
{
int arr[] = { 2 , 10 , 7 , 18 , 5 , 33 , 0 };
int n = arr.length;
int m = 3 , k = 1 ;
int presum[] = new int [n + 1 - k] ;
calculatePresumArray(presum, arr, n, k);
System.out.println(maxSumMnonOverlappingSubarray(presum,
m, n + 1 - k, k, 0 ));
}
}
|
Python
def calculatePresumArray(presum,arr, n, k):
for i in range (k):
presum[ 0 ] + = arr[i]
for i in range ( 1 ,n - k + 1 ):
presum[i] + = presum[i - 1 ] + arr[i + k - 1 ] - arr[i - 1 ]
def maxSumMnonOverlappingSubarray(presum, m, size, k, start):
if (m = = 0 ):
return 0
if (start > size - 1 ):
return 0
mx = 0
includeMax = presum[start] + maxSumMnonOverlappingSubarray(presum,m - 1 , size, k, start + k)
excludeMax = maxSumMnonOverlappingSubarray(presum,m, size, k, start + 1 )
return max (includeMax, excludeMax)
arr = [ 2 , 10 , 7 , 18 , 5 , 33 , 0 ]
n = len (arr)
m, k = 3 , 1
presum = [ 0 for i in range (n + 1 - k)]
calculatePresumArray(presum, arr, n, k)
print (maxSumMnonOverlappingSubarray(presum, m, n + 1 - k, k, 0 ))
|
C#
using System;
class GFG {
static void calculatePresumArray( int []presum,
int []arr, int n, int k)
{
for ( int i = 0; i < k; i++)
presum[0] += arr[i];
for ( int i = 1; i <= n - k; i++)
presum[i] += presum[i - 1] + arr[i + k - 1]
- arr[i - 1];
}
static int maxSumMnonOverlappingSubarray(
int []presum, int m, int size,
int k, int start)
{
if (m == 0)
return 0;
if (start > size - 1)
return 0;
int includeMax = presum[start] +
maxSumMnonOverlappingSubarray(presum,
m - 1, size, k, start + k);
int excludeMax =
maxSumMnonOverlappingSubarray(presum,
m, size, k, start + 1);
return Math.Max(includeMax, excludeMax);
}
public static void Main ()
{
int []arr = { 2, 10, 7, 18, 5, 33, 0 };
int n = arr.Length;
int m = 3, k = 1;
int []presum = new int [n + 1 - k] ;
calculatePresumArray(presum, arr, n, k);
Console.WriteLine(
maxSumMnonOverlappingSubarray(presum,
m, n + 1 - k, k, 0));
}
}
|
Javascript
<script>
function calculatePresumArray(presum,arr,n,k)
{
for (let i = 0; i < k; i++)
presum[0] += arr[i];
for (let i = 1; i <= n - k; i++)
presum[i] += presum[i - 1] + arr[i + k - 1] -
arr[i - 1];
}
function maxSumMnonOverlappingSubarray(presum,m,size,k,start)
{
if (m == 0)
return 0;
if (start > size - 1)
return 0;
let mx = 0;
let includeMax = presum[start] +
maxSumMnonOverlappingSubarray(presum,
m - 1, size, k, start + k);
let excludeMax =
maxSumMnonOverlappingSubarray(presum,
m, size, k, start + 1);
return Math.max(includeMax, excludeMax);
}
let arr=[2, 10, 7, 18, 5, 33, 0];
let n = arr.length;
let m = 3, k = 1;
let presum = new Array(n + 1 - k) ;
for (let i = 0; i < presum.length; i++)
{
presum[i] = 0;
}
calculatePresumArray(presum, arr, n, k);
document.write(maxSumMnonOverlappingSubarray(presum,
m, n + 1 - k, k, 0));
</script>
|
Last Updated :
03 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...