Minimum Array Sum - Solution & Explanation
Problem Statement
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.
Example 1:
Input: nums = [2,8,3,19,3], k = 3, op1 = 1, op2 = 1
Output: 23
Explanation:
- Apply Operation 2 to
nums[1] = 8, makingnums[1] = 5. - Apply Operation 1 to
nums[3] = 19, makingnums[3] = 10. - The resulting array becomes
[2, 5, 3, 10, 3], which has the minimum possible sum of 23 after applying the operations.
Example 2:
Input: nums = [2,4,3], k = 3, op1 = 2, op2 = 1
Output: 3
Explanation:
- Apply Operation 1 to
nums[0] = 2, makingnums[0] = 1. - Apply Operation 1 to
nums[1] = 4, makingnums[1] = 2. - Apply Operation 2 to
nums[2] = 3, makingnums[2] = 0. - The resulting array becomes
[1, 2, 0], which has the minimum possible sum of 3 after applying the operations.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 1050 <= k <= 1050 <= op1, op2 <= nums.length
Approach Overview
Problem Overview: You are given an array of integers and a limited number of operations that modify element values. Each operation reduces a number in a specific way (for example halving it or subtracting a constant). The goal is to apply these operations optimally so the total array sum becomes as small as possible.
Approach 1: Greedy with Priority Queue (O((n + k) log n) time, O(n) space)
The greedy strategy always applies the operation that produces the largest immediate reduction in value. Push all elements into a max heap so the largest element is processed first. For each available operation, pop the current maximum, compute the reduction produced by the allowed operation, update the value, and push it back into the heap. This works well when the best choice at each step is the element producing the biggest decrease in the sum. The heap ensures efficient extraction and reinsertion while keeping the array effectively ordered by impact.
This approach is intuitive and fast in practice. You repeatedly evaluate which operation gives the maximum decrease and apply it to the largest candidate. Since heap operations take O(log n), the total cost depends on the number of operations performed.
Approach 2: Dynamic Programming (O(n * k1 * k2) time, O(n * k1 * k2) space)
When operations interact or their order matters, greedy choices may not always lead to the global minimum. Dynamic programming models every valid state explicitly. Define a DP state representing the minimum sum achievable after processing the first i elements while using a certain number of operations of each type. For each element, evaluate all valid combinations: no operation, operation A, operation B, or both if allowed.
Transitions update the state by applying the operation’s transformation to the current number and adding the resulting value to the accumulated sum. The DP table ensures every combination of operations is considered exactly once, guaranteeing the globally minimal sum. This approach is more expensive but reliable when greedy assumptions break.
The state transitions iterate through elements and update remaining operation counts. This makes the solution deterministic and safe for edge cases where applying a smaller local reduction enables a larger future gain.
Recommended for interviews: Start by explaining the greedy intuition since minimizing the largest contributors to the sum is a natural first idea. Then discuss why overlapping operation choices may require a dynamic programming formulation. Interviewers typically expect you to recognize the greedy optimization quickly, but implementing a correct DP over an array with operation counts demonstrates stronger problem‑solving depth.
Approach 1: Greedy Approach
In this approach, we aim to minimize the array sum by applying operations in a greedy fashion. The main idea is to sort the array and apply operations that give the maximum immediate decrement in the array sum, prioritized on larger numbers first.
Steps:
- Sort the array in descending order to focus on larger numbers first and apply operations that reduce them significantly.
- Use Operation 1 on the largest unused value, ensuring that it does not exceed the total ops count limit, op1.
- Apply Operation 2 only if it can make a significant decrement and does not exceed op2.
- Iterate through the array, applying possible operations until no more are left.
This way, the operations have maximal reduction impact on the sum.
The code sorts the array in descending order and tries to apply the operations with the highest reduction in value first. It first applies the division operation if it can be applied, then the subtraction if it still can meaningfully reduce a value.
Code
Python
JavaScript
Complexity
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) besides the input array.
Approach 2: Dynamic Programming
This approach uses dynamic programming to find a minimal configuration for the sum by exploring all combinations of operations applied.
Steps:
- Define a DP table to store the minimal sum possible with the number of operations applied to each index.
- For each element, the DP decision is between applying operation 1 or 2 or applying neither, checking for any valid reduction.
- Transfer previous states and update the current state with decisions that result in minimizing the sum.
This allows tracking the impact of each operation on the whole array efficiently.
This Java code uses the DP array to track minimal arbitrary application of operations. Each cell represents a sum state given the operations performed up to that moment.
Complexity
Time Complexity: O(n*op1*op2). In this form, it iterates over op1 and op2 for each number.
Space Complexity: O(op1*op2) for the DP table.
Complexity Comparison
| Approach | Complexity |
|---|---|
| Greedy Approach | Time Complexity: O(n log n) due to sorting. |
| Dynamic Programming | Time Complexity: O(n*op1*op2). In this form, it iterates over op1 and op2 for each number. |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy with Max Heap | O((n + k) log n) | O(n) | When each operation independently reduces the largest element and greedy decisions are optimal |
| Dynamic Programming | O(n * k1 * k2) | O(n * k1 * k2) | When operations interact or their order affects the final result |
Video Solution
Minimum Array Sum | Detailed for Beginners | Contest Problem | Leetcode 3366 | codestorywithMIK • codestorywithMIK • 5,022 views views
Watch 6 more video solutions →Frequently Asked Questions
Is Minimum Array Sum easy or hard?
Minimum Array Sum Python/Java solution
How to solve Minimum Array Sum in O(n)?
What is the best approach for Minimum Array Sum?
Is Minimum Array Sum asked at Google/Amazon/Meta?
What data structure is used in Minimum Array Sum?
What is the time complexity of Minimum Array Sum?
Ready to solve this problem?
Practice Minimum Array Sum with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor