
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find maximum sum array of length less than or equal to m in C++
In the problem, we are given n arrays of different length. Our task is to Find the maximum sum array of length less than or equal to m.
We need to find sub-arrays from the arrays to maximise the sum and make the length of all subarrays combined equal to m.
Let’s take an example to understand the problem,
Input
n = 3, m = 4 arrOfArr[][] = { {5, 2, -1, 4, -3} {3, -2, 1, 6} {-2, 0, 5} }
Output
20
Explanation
SubArrays are {5, 4}, {6}, {5}, length = 2 + 1 + 1 = 4 Sum = 5 + 4 + 6 + 5 = 20
Solution Approach
The problem can be solved using a dynamic programming approach. We will create a DP array and compute the cumulative array sums of length k which varies from 0 to m.
In the DP array, store the maximum array sum for all lengths from 0 to m for each array in the 2D DP. Then return the maximum sum which will be at the last row of the array.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; #define N 5 int findMax(int a, int b){ if(a > b) return a; return b; } int findMaxSumArray(int arr[][N], int M) { int DP[N][M]; int sumArray[M]; int sum[M]; memset(DP, -1, sizeof(DP[0][0]) * N * M); sumArray[0] = 0; DP[0][0] = 0; for (int i = 1; i <= 5; i++) { int len = arr[i - 1][0]; for (int j = 1; j <= len; j++) { sumArray[j] = arr[i - 1][j]; sumArray[j] += sumArray[j - 1]; sum[j] = -100; } for (int j = 1; j <= len && j <= 6; j++) for (int k = 1; k <= len; k++) if (j + k - 1 <= len) sum[j] = findMax(sum[j], sumArray[j + k - 1] - sumArray[k - 1]); for (int j = 0; j <= 6; j++) DP[i][j] = DP[i - 1][j]; for (int j = 1; j <= 6; j++) for (int cur = 1; cur <= j && cur <= len; cur++) DP[i][j] = findMax(DP[i][j], DP[i - 1][j - cur] + sum[cur]); } int maxSum = 0; for (int i = 0; i <= 6; i++) maxSum = findMax(maxSum, DP[5][i]); return maxSum; } int main() { int arr[][N] = { { 3, 2, -1, 6 }, { 2, 7, -1 }, { 3, 2, 2, -4 } }; int m = 4; cout<<"Maximum sum array of length less than or equal to "<<m<<" : "<<findMaxSumArray(arr, m); }
Output
Maximum sum array of length less than or equal to 4 : 15
- Related Articles
- Maximum Side Length of a Square with Sum Less than or Equal to Threshold in C++
- Maximum sum subarray having sum less than or equal to given sums in C++
- Find maximum product of digits among numbers less than or equal to N in C++
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++
- Print triplets with sum less than or equal to k in C Program
- Find three integers less than or equal to N such that their LCM is maximum - C++
- Find three integers less than or equal to N such that their LCM is maximum in C++
- Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++
- Find Multiples of 2 or 3 or 5 less than or equal to N in C++
- How to find numbers in an array that are greater than, less than, or equal to a value in java?
- Find all factorial numbers less than or equal to n in C++
- Mask an array where less than or equal to a given value in Numpy
- Linear magnification produced by a concave mirror may be:(a) less than 1 or equal to 1 (b) more than 1 or equal to 1(c) less than 1, more than 1 or equal to 1 (d) less than 1 or more than 1
- Linear magnification produced by a convex lens can be:(a) less than 1 or more than 1.(b) less than 1 or equal to 1.(c) more than 1 or equal to 1.(d) less than 1, equal to 1 or more than 1.
- Complete the following statements:The probability of an event is greater than or equal to …………. and less than or equal to ………..

Advertisements