Find maximum sum array of length less than or equal to m
Given n arrays of different length consisting of integers, the target is to pick atmost one subarray from an array such that the combined length of all picked subarrays does not become greater than m and also sum of their elements is maximum.(also given that value of n can not be more than 100)
Prerequisite : Knapsack Problem
Examples :
Input :
n = 5, m = 6
arr[][m] = {{3, 2, 3, 5},
{2, 7, -1},
{2, 8, 10},
{4, 5, 2, 6, 1},
{3, 2, 3, -2}};
Output : Maximum sum can be obtained is 39
Explanation : We are allowed to pick at most
one subarray from every array.
We get total sum 39 as ((5) + (7) + (8 + 10) +
(4 + 5))
Input :
n = 3, m = 4
arr[][m] = {{2, 3, 2},
{3, -1, 7, 10},
{4, 8, 10, -5, 3}};
Output : Maximum sum can be obtained is 35
This problem is similar to Knapsack problem.where you have to either pick an element or leave it. We will have the same strategy here.
Given that the total number of elements in these n arrays is atmost 10^5. It is also known that m is atmost 10^3 and the input arrays can contain negative numbers. First, make a DP table (2D array) of size n * m and then, pre-compute the cumulative sum of an array so that maximum sum of every length from 1 to n of that array can be easily computed, so that for every given array there can be a maximum contiguous sum for length k, where k is from 1 to length of the array.
In detail, process input arrays one by one. First, compute the maximum sum subarrays of processed array for all sizes from 1 to length. Then, update our dynamic programming table with these values and we start processing the next array.
Algorithm :
- Pick one array from n arrays and start processing it.
- Calculate maximum contiguous sum for length k, k is from 1 to length of the array and save it in the array maxSum.
- Now, fill the DP table by storing maximum sum possible for every length 0 to m.
- In the last step we traverse last row(nth row) of DP table and pick maximum sum possible and return it.
Below is the implementation of above approach :
C++
// A Dynamic Programming based C++ program to find// maximum sum of array of size less than or// equal to m from given n arrays#include <bits/stdc++.h>usingnamespacestd;/* N and M to define sizes of arr,dp, current_arr and maxSum */#define N 105#define M 1001// INF to define min value#define INF -1111111111// Function to find maximum sumintmaxSum(intarr[][N]){// dp array of size N x Mintdp[N][M];// current_arr of size Mintcurrent_arr[M];// maxsum of size Mintmaxsum[M];memset(dp, -1,sizeof(dp[0][0]) * N * M);current_arr[0] = 0;// if we have 0 elements// from 0th arraydp[0][0] = 0;for(inti = 1; i <= 5; i++) {intlen = arr[i - 1][0];// compute the cumulative sum arrayfor(intj = 1; j <= len; j++) {current_arr[j] = arr[i - 1][j];current_arr[j] += current_arr[j - 1];maxsum[j] = INF;}// calculating the maximum contiguous// array for every length j, j is from// 1 to lengtn of the arrayfor(intj = 1; j <= len && j <= 6; j++)for(intk = 1; k <= len; k++)if(j + k - 1 <= len)maxsum[j] = max(maxsum[j],current_arr[j + k - 1] -current_arr[k - 1]);// every state is depending on// its previous statefor(intj = 0; j <= 6; j++)dp[i][j] = dp[i - 1][j];// computation of dp table similar// approach as knapsack problemfor(intj = 1; j <= 6; j++)for(intcur = 1; cur <= j && cur <= len; cur++)dp[i][j] = max(dp[i][j],dp[i - 1][j - cur] +maxsum[cur]);}// now we have done processing with// the last array lets find out// what is the maximum sum possibleintans = 0;for(inti = 0; i <= 6; i++)ans = max(ans, dp[5][i]);returnans;}// Driver programintmain(){// first element of each// row is the size of that rowintarr[][N] = { { 3, 2, 3, 5 },{ 2, 7, -1 },{ 2, 8, 10 },{ 4, 5, 2, 6, 1 },{ 3, 2, 3, -2 } };cout <<"Maximum sum can be obtained "<<"is : "<< maxSum(arr) <<"\n";}Java
// Java program to find maximum sum// of array of size less than or// equal to m from given n arraysimportjava.io.*;publicclassGFG{/* N and M to definesizes of arr,dp, current_arrand maxSum */staticintN =105;staticintM =1001;// INF to define// min valuestaticintINF = -1111111111;// Function to find// maximum sumstaticintmaxSum(int[][]arr){// dp array of size N x Mint[][]dp =newint[N][M];// current_arr of size Mint[]current_arr =newint[M];// maxsum of size Mint[]maxsum =newint[M];for(inti =0; i < N; i++){for(intj =0; j < M ; j++)dp[i][j] = -1;}current_arr[0] =0;// if we have 0 elements// from 0th arraydp[0][0] =0;for(inti =1; i <=5; i++){intlen = arr[i -1][0];// compute the cumulative// sum arrayfor(intj =1; j <= len; j++){current_arr[j] = arr[i -1][j];current_arr[j] += current_arr[j -1];maxsum[j] = INF;}// calculating the maximum// contiguous array for every// length j, j is from 1 to// lengtn of the arrayfor(intj =1; j <= len && j <=6; j++)for(intk =1; k <= len; k++)if(j + k -1<= len)maxsum[j] = Math.max(maxsum[j],current_arr[j + k -1] -current_arr[k -1]);// every state is depending// on its previous statefor(intj =0; j <=6; j++)dp[i][j] = dp[i -1][j];// computation of dp table// similar approach as// knapsack problemfor(intj =1; j <=6; j++)for(intcur =1; cur <= j &&cur <= len; cur++)dp[i][j] = Math.max(dp[i][j],dp[i -1][j - cur] +maxsum[cur]);}// now we have done processing// with the last array lets// find out what is the maximum// sum possibleintans =0;for(inti =0; i <=6; i++)ans = Math.max(ans, dp[5][i]);returnans;}// Driver Codepublicstaticvoidmain(String args[]){// first element of each// row is the size of that rowint[][] arr = {{3,2,3,5},{2,7, -1},{2,8,10},{4,5,2,6,1},{3,2,3, -2}};System.out.println("Maximum sum can be "+"obtained is : "+maxSum(arr));}}// This code is contributed by// Manish Shaw(manishshaw1)Python3
# A Dynamic Programming based Python3# program to find maximum sum of array# of size less than or equal to m from# given n arrays# N and M to define sizes of arr,# dp, current_arr and maxSumN=105M=1001# INF to define min valueINF=-1111111111# Function to find maximum sumdefmaxSum(arr):# dp array of size N x Mdp=[[-1forxinrange(M)]foryinrange(N)]# current_arr of size Mcurrent_arr=[0]*M# maxsum of size Mmaxsum=[0]*Mcurrent_arr[0]=0# If we have 0 elements# from 0th arraydp[0][0]=0foriinrange(1,6):len=arr[i-1][0]# Compute the cumulative sum arrayforjinrange(1,len+1):current_arr[j]=arr[i-1][j]current_arr[j]+=current_arr[j-1]maxsum[j]=INF# Calculating the maximum contiguous# array for every length j, j is from# 1 to lengtn of the arrayj=1whilej <=lenandj <=6:forkinrange(1,len+1):if(j+k-1<=len):maxsum[j]=max(maxsum[j],current_arr[j+k-1]-current_arr[k-1])j+=1# Every state is depending on# its previous stateforjinrange(7):dp[i][j]=dp[i-1][j]# computation of dp table similar# approach as knapsack problemforjinrange(1,7):cur=1whilecur <=jandcur <=len:dp[i][j]=max(dp[i][j],dp[i-1][j-cur]+maxsum[cur])cur+=1# Now we have done processing with# the last array lets find out# what is the maximum sum possibleans=0foriinrange(7):ans=max(ans, dp[5][i])returnans# Driver codeif__name__=="__main__":# First element of each# row is the size of that rowarr=[ [3,2,3,5],[2,7,-1],[2,8,10],[4,5,2,6,1],[3,2,3,-2] ]print("Maximum sum can be obtained","is : ", maxSum(arr))# This code is contributed by chitranayalC#
// C# program to find maximum sum// of array of size less than or// equal to m from given n arraysusingSystem;classGFG{/* N and M to definesizes of arr,dp, current_arrand maxSum */staticintN = 105;staticintM = 1001;// INF to define// min valuestaticintINF = -1111111111;// Function to find// maximum sumstaticintmaxSum(int[][]arr){// dp array of size N x Mint[,]dp =newint[N, M];// current_arr of size Mint[]current_arr =newint[M];// maxsum of size Mint[]maxsum =newint[M];for(inti = 0; i < N; i++){for(intj = 0; j < M ; j++)dp[i, j] = -1;}current_arr[0] = 0;// if we have 0 elements// from 0th arraydp[0, 0] = 0;for(inti = 1; i <= 5; i++){intlen = arr[i - 1][0];// compute the cumulative// sum arrayfor(intj = 1; j <= len; j++){current_arr[j] = arr[i - 1][j];current_arr[j] += current_arr[j - 1];maxsum[j] = INF;}// calculating the maximum// contiguous array for every// length j, j is from 1 to// lengtn of the arrayfor(intj = 1; j <= len && j <= 6; j++)for(intk = 1; k <= len; k++)if(j + k - 1 <= len)maxsum[j] = Math.Max(maxsum[j],current_arr[j + k - 1] -current_arr[k - 1]);// every state is depending// on its previous statefor(intj = 0; j <= 6; j++)dp[i, j] = dp[i - 1, j];// computation of dp table// similar approach as// knapsack problemfor(intj = 1; j <= 6; j++)for(intcur = 1; cur <= j &&cur <= len; cur++)dp[i, j] = Math.Max(dp[i, j],dp[i - 1, j - cur] +maxsum[cur]);}// now we have done processing// with the last array lets// find out what is the maximum// sum possibleintans = 0;for(inti = 0; i <= 6; i++)ans = Math.Max(ans, dp[5, i]);returnans;}// Driver CodestaticvoidMain(){// first element of each// row is the size of that rowint[][] arr =newint[][]{newint[]{ 3, 2, 3, 5 },newint[]{ 2, 7, -1 },newint[]{ 2, 8, 10 },newint[]{ 4, 5, 2, 6, 1 },newint[]{ 3, 2, 3, -2 }};Console.Write ("Maximum sum can be "+"obtained is : "+maxSum(arr) +"\n");}}// This code is contributed by// Manish Shaw(manishshaw1)Output :Maximum sum can be obtained is : 39
Time complexity : O(
n)

