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
iand dividenums[i]by 2, rounding up to the nearest whole number. You can perform this operation at mostop1times, and not more than once per index. - Operation 2: Choose an index
iand subtractkfromnums[i], but only ifnums[i]is greater than or equal tok. You can perform this operation at mostop2times, 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code