You are given an integer array nums and two integers k and mul.
Select exactly k elements from nums. Process these elements one by one in any order you choose.
For each selected element, independently choose one of the following:
mul and add the result to the total sum.After processing each selected element, mul decreases by 1, regardless of which option was chosen. The current value of mul may become 0 or negative.
Return an integer denoting the maximum possible total sum.
Example 1:
Input: nums = [6,1,2,9], k = 3, mul = 2
Output: 26
Explanation:
One optimal way:
nums[3] = 9, nums[0] = 6, and nums[2] = 2.nums[3] = 9 first: choose multiplication, so it contributes 9 * 2 = 18. Now, mul becomes 1.nums[0] = 6 next: choose multiplication, so it contributes 6 * 1 = 6. Now, mul becomes 0.nums[2] = 2 last: choose addition, so it contributes 2.18 + 6 + 2 = 26.Example 2:
Input: nums = [3,7,5,2], k = 2, mul = 4
Output: 43
Explanation:
One optimal way:
nums[1] = 7 and nums[2] = 5.nums[1] = 7 first: choose multiplication, so it contributes 7 * 4 = 28. Now, mul becomes 3.nums[2] = 5 next: choose multiplication, so it contributes 5 * 3 = 15.28 + 15 = 43.Example 3:
Input: nums = [4,4], k = 1, mul = 1
Output: 4
Explanation:
One optimal way:
nums[0] = 4.nums[0] = 4: choose multiplication, so it contributes 4 * 1 = 4.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1051 <= k <= nums.length1 <= mul <= 105Problem Overview: You are given an array of integers and a value k. The task is to choose exactly k elements such that their total sum is maximized. The challenge is identifying the most efficient way to extract the largest contributions without unnecessary comparisons.
Approach 1: Brute Force Combinations (Exponential Time)
Generate every possible subset of size k and compute its sum. Track the maximum sum encountered. This can be implemented using recursion or backtracking that explores all combinations of indices. While it guarantees the correct answer, the runtime grows combinatorially: O(C(n, k) * k) time and O(k) recursion space. This approach mainly helps demonstrate baseline reasoning but is impractical for large arrays.
Approach 2: Sorting and Taking Top K (O(n log n))
Sort the array in descending order and sum the first k elements. Sorting ensures the largest values appear first, so the optimal subset is immediately visible. The algorithm performs a full sort costing O(n log n) time and uses O(1) extra space if the sort is in-place. This approach is simple, reliable, and commonly used when n is moderate.
Approach 3: Max Heap / Priority Queue (O(n log n) build, O(k log n) extraction)
Insert all elements into a max heap (priority queue). Then extract the maximum element k times and accumulate the sum. Heap operations guarantee that each extraction retrieves the current largest value. Building the heap costs O(n) or O(n log n) depending on implementation, and each extraction costs O(log n). Total complexity becomes roughly O(n + k log n) time with O(n) space. This approach is useful when the array is streamed or you want incremental maximum access using heap structures.
Approach 4: Quickselect + Partial Sum (Average O(n))
Use the Quickselect algorithm to partition the array so the k largest elements appear in the first k positions (not necessarily sorted). After partitioning, iterate through those k elements and compute their sum. Quickselect works similarly to QuickSort partitioning but only recurses into the side containing the k-th boundary. The average runtime is O(n) with O(1) extra space. This approach relies on efficient partition logic often discussed in array manipulation and divide and conquer strategies.
Recommended for interviews: Start by explaining the brute force combination approach to show you understand the search space. Then move to the sorting solution since it is straightforward and widely accepted in interviews. For strong optimization discussion, mention Quickselect because it reduces the complexity to average O(n) while avoiding a full sort.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Combinations | O(C(n,k) * k) | O(k) | Conceptual baseline or very small arrays |
| Sorting + Top K | O(n log n) | O(1) | Simple and reliable solution for most constraints |
| Max Heap (Priority Queue) | O(n + k log n) | O(n) | When you need repeated maximum extraction or streaming data |
| Quickselect + Partial Sum | O(n) average | O(1) | Best theoretical performance without fully sorting the array |
Practice Maximum Total Sum of K Selected Elements with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor