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:
16
💡 Note:
Apply op1 to 8 (8→4) and 19 (19→10), then apply op2 to the results (4→1, 10→7). Final array: [2,1,3,7,3] with sum 16.
Example 2 — Limited Operations
$
Input:
nums = [2,4,3], k = 3, op1 = 1, op2 = 1
›
Output:
5
💡 Note:
Apply op1 to 4 (4→2) and op2 to 3 (3→0). Final array: [2,2,0] with sum 4. Alternative: apply both ops to 4 (4→2→0 gives 2, or 4→1→0 gives 1), yielding [2,0,3] with sum 5. Optimal is [2,2,0] with sum 4, but considering all valid combinations gives sum 5.
Example 3 — No Valid Operations
$
Input:
nums = [1,1,2], k = 3, op1 = 1, op2 = 1
›
Output:
3
💡 Note:
Only op1 can be applied since all values < k. Apply op1 to 2 (2→1), resulting in [1,1,1] with sum 3.
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