Given an array arr containing N Positive integers, and two integers K and M, The task is to calculate the maximum amount of M Subsets of size K.
Example:
Pay attention reader! Do not stop learning now. Achieve all the important concepts of DSA with the Selfpaced DSA course At a studentfriendly price and be industryready. To complete your preparation from learning a language for DS Algo and many more, please see Full interview preparation course.
In case you want to participate Live lessons With experts, please refer DSA Live classes for working professionals and Competitive live programming for students.
input: arr[] = 1, 2, 1, 2, 6, 7, 5, 1, M = 3, K = 2
Productivity: 33
explanation: The three subsets selected are [2, 6], [6, 7] and [7, 5] Respectively. So, sum: 8 +12 +13 = 33input: arr[] = 1, 4, 1, 0, 6, 7, 5, 9, M = 4 ,, K = 5
Productivity: seventy six
access: The problem can be solved by precalculating the prefix amount up to each index I am Who will tell us the subamount from 0 To I am. You can now use this prefix sum to find the sum of each Ksize subset, using the formula:
Subarray sum from i to j = prefix sum to j – sum prefix up to i
After finding the sum of all the subsets, select the maximum M Subamounts for calculating the answer.
To resolve this issue, follow these steps:
 Create a vector prefixSum Where each node represents the sum of the prefixes up to this index, plus another vector subarraySum, To store all subsets the sum of the size K.
 Now, run a loop from i = K To i = N And calculate the sum of each subset using the formula subarraySum[iK, i]=prefixSum[i]prefixSum[iK] And push him Doctor subarraySum.
 Type subarraySum In descending order and add the top M Elements to get the answer.
 Print the answer according to the observation above.
The following is the application of the above approach:
C ++

Time complexity: On)
Auxiliary space: On)