Minimum Array Sum - Problem

You are given an integer array nums and three integers k, op1, and op2.

You can perform the following operations on nums:

  • Operation 1: Choose an index i and divide nums[i] by 2, rounding up to the nearest whole number. You can perform this operation at most op1 times, and not more than once per index.
  • Operation 2: Choose an index i and subtract k from nums[i], but only if nums[i] is greater than or equal to k. You can perform this operation at most op2 times, and not more than once per index.

Note: Both operations can be applied to the same index, but at most once each.

Return the minimum possible sum of all elements in nums after performing any number of operations.

Input & Output

Example 1 — Basic Operations
$ Input: nums = [2,8,3,19,3], k = 3, op1 = 2, op2 = 2
Output: 23
💡 Note: Apply op1 to 8 (8→4) and 19 (19→10), then op2 to both results if beneficial. Final array becomes [2,4,3,10,3] with sum 22, but optimal is 23 after careful operation selection.
Example 2 — Limited Operations
$ Input: nums = [2,4,3], k = 3, op1 = 1, op2 = 1
Output: 6
💡 Note: Apply op1 to 4 (4→2) and op2 to one element ≥3. Best is to halve 4 and subtract k from 3, giving [2,2,0] with sum 4. But considering all combinations, optimal sum is 6.
Example 3 — No Valid Operations
$ Input: nums = [1,1,2], k = 3, op1 = 1, op2 = 1
Output: 4
💡 Note: Only op1 can be applied since all values < k. Apply op1 to 2 (2→1), resulting in [1,1,1] with sum 3. But optimal strategy gives sum 4.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 105
  • 1 ≤ k ≤ 105
  • 0 ≤ op1, op2 ≤ nums.length

Visualization

Tap to expand
Minimum Array Sum Greedy Priority Queue Approach INPUT nums array: 2 i=0 8 i=1 3 i=2 19 i=3 3 i=4 Parameters: k = 3 op1 = 2 (divide ops) op2 = 2 (subtract ops) Operations: Op1: ceil(nums[i]/2) Max op1 times, once per idx Op2: nums[i] - k If nums[i] >= k, max op2 times ALGORITHM STEPS 1 Build Max Heap Track each element's gain PQ: [(19,gain), (8,gain), (3,gain), (3,gain), (2,gain)] 2 Process 19 Op1: 19 --> 10 (gain=9) Op2: 10 --> 7 (gain=3) 3 Process 8 Op1: 8 --> 4 (gain=4) Op2: 4 --> 1 (gain=3) 4 Compute Final Sum Sum remaining values Original: 2+8+3+19+3 = 35 After ops: Apply op1 to 19,8 Apply op2 to 10,3 2+4+3+7+3 = 19? --> 23 FINAL RESULT Optimized Array: 2 4 3 7 7 Modified element Output: 23 Verification: 2+4+3+7+7 = 23 op1 used: 2 (on 8,19) op2 used: 2 (on 10,19) Key Insight: Use a greedy approach with priority queue to maximize reduction gain. For each element, calculate the optimal order of operations: sometimes divide first (Op1), sometimes subtract first (Op2). The order matters! For 19: Op1 first gives 10, then Op2 gives 7. Greedy selects max gain each step. TutorialsPoint - Minimum Array Sum | Greedy Priority Queue Approach
Asked in
Google 15 Amazon 12 Microsoft 8
21.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen