Skip to main content

Minimum Array Sum - Solution & Explanation

MediumArrayDynamic Programming17 min readAsked at: Amazon, Microsoft, Oracle +2
Practice this problem

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 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.

 

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, making nums[1] = 5.
  • Apply Operation 1 to nums[3] = 19, making nums[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, making nums[0] = 1.
  • Apply Operation 1 to nums[1] = 4, making nums[1] = 2.
  • Apply Operation 2 to nums[2] = 3, making nums[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 <= 100
  • 0 <= nums[i] <= 105
  • 0 <= k <= 105
  • 0 <= 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:

  1. Sort the array in descending order to focus on larger numbers first and apply operations that reduce them significantly.
  2. Use Operation 1 on the largest unused value, ensuring that it does not exceed the total ops count limit, op1.
  3. Apply Operation 2 only if it can make a significant decrement and does not exceed op2.
  4. 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.

Try this approach in the editor →

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:

  1. Define a DP table to store the minimal sum possible with the number of operations applied to each index.
  2. For each element, the DP decision is between applying operation 1 or 2 or applying neither, checking for any valid reduction.
  3. 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.

Code

Java

C#

Python

C++

Go

TypeScript

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.

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Greedy Approach

Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) besides the input array.

Dynamic Programming

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.

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Greedy with Max HeapO((n + k) log n)O(n)When each operation independently reduces the largest element and greedy decisions are optimal
Dynamic ProgrammingO(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 is considered a medium difficulty problem. The greedy idea is straightforward, but handling multiple operations and guaranteeing the minimal sum can require dynamic programming and careful state transitions.
Minimum Array Sum Python/Java solution
Python and JavaScript implementations commonly use the greedy heap strategy with priority queues. Java and C# implementations often show the dynamic programming formulation that tracks operation counts per index and computes the minimum achievable sum.
How to solve Minimum Array Sum in O(n)?
An exact O(n) solution is usually not possible because operations must repeatedly target the largest elements, which requires ordering. Using a max heap gives O(log n) updates, resulting in O((n + k) log n). Linear time would require constraints where operations can be computed directly without reordering.
What is the best approach for Minimum Array Sum?
The most practical solution uses a greedy strategy with a max heap. Always apply the operation that produces the largest reduction on the current maximum element. This reduces the total sum quickly and runs in about O((n + k) log n) time due to heap operations.
Is Minimum Array Sum asked at Google/Amazon/Meta?
Problems combining greedy optimization and dynamic programming frequently appear in interviews at companies like Amazon, Google, and Meta. Variants involving minimizing sums after limited operations or resource allocation are common interview patterns.
What data structure is used in Minimum Array Sum?
The greedy solution typically uses a max heap (priority queue) to quickly retrieve and update the largest element after each operation. The dynamic programming approach uses a multidimensional DP table to track the minimum sum after applying different counts of operations.
What is the time complexity of Minimum Array Sum?
The greedy heap solution typically runs in O((n + k) log n), where n is the array size and k is the number of operations performed. A dynamic programming alternative that tracks operation counts can take O(n * k1 * k2) time depending on how many operations of each type are allowed.

Ready to solve this problem?

Practice Minimum Array Sum with our built-in code editor and test cases.

Practice on FleetCode