- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of numbers called nums, and another value k, we have to find the k non-overlapping, non-empty sublists such that the sum of their sums is maximum. We can consider k is less than or equal to the size of nums.

So, if the input is like nums = [11, -1, 2, 1, 6, -24, 11, -9, 6] k = 3, then the output will be 36, as we can select the sublists [11, -1, 2, 1, 6], [11], and [6] to get sums of [19, 11, 6] = 36.

To solve this, we will follow these steps −

- n := size of nums
- if n is same as 0 or k is same as 0, then −
- return 0

- Define an array hi of size k + 1 and fill with -inf,
- Define another array open of size k + 1 and fill with -inf
- hi[0] := 0
- for each num in nums −
- Define an array nopen of size k + 1 and fill with -inf
- for initialize i := 1, when i <= k, update (increase i by 1), do
- if open[i] > -inf, then −
- nopen[i] := open[i] + num

- if hi[i - 1] > -inf, then −
- nopen[i] := maximum of nopen[i] and hi[i - 1] + num

- if open[i] > -inf, then −
- open := move(nopen)
- for initialize i := 1, when i <= k, update (increase i by 1), do
- hi[i] := maximum of hi[i] and open[i]

- return hi[k]

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int solve(vector<int>& nums, int k) { int n = nums.size(); if (n == 0 || k == 0) return 0; vector<int> hi(k + 1, INT_MIN), open(k + 1, INT_MIN); hi[0] = 0; for (int num : nums) { vector<int> nopen(k + 1, INT_MIN); for (int i = 1; i <= k; ++i) { if (open[i] > INT_MIN) nopen[i] = open[i] + num; if (hi[i - 1] > INT_MIN) nopen[i] = max(nopen[i], hi[i - 1] + num); } open = move(nopen); for (int i = 1; i <= k; ++i) hi[i] = max(hi[i], open[i]); } return hi[k]; } int main(){ vector<int> v = {11, -1, 2, 1, 6, -24, 11, -9, 6}; int k = 3; cout << solve(v, 3); }

{11, -1, 2, 1, 6, -24, 11, -9, 6}, 3

36

- Related Questions & Answers
- Program to find maximum sum of two non-overlapping sublists in Python
- Program to find largest sum of 3 non-overlapping sublists of same sum in Python
- Maximum Sum of 3 Non-Overlapping Subarrays in C++
- Maximum Sum of Two Non-Overlapping Subarrays in C++
- Program to find minimum largest sum of k sublists in C++
- Program to find number of sublists whose sum is given target in python
- Maximum sum two non-overlapping subarrays of given size in C++
- Max sum of M non-overlapping subarrays of size K in C++
- Maximum sum of lengths of non-overlapping subarrays with k as the max element in C++
- Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python
- C++ program to find length of non empty substring whose sum is even
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Program to find sum of rectangle whose sum at most k in Python
- C++ program to find maximum possible median of elements whose sum is s
- Program to find sum of the sum of all contiguous sublists in Python

Advertisements