You are given an integer array nums.
You can perform the following operation any number of times:
a and b such that nums[a] % nums[b] == 0.nums[a] with nums[b].Return the minimum possible sum of the array after performing any number of operations.
Example 1:
Input: nums = [3,6,2]
Output: 7
Explanation:
a = 1, b = 2, where nums[a] = 6 and nums[b] = 2. Since 6 % 2 == 0, replace nums[1] with nums[2].[3, 2, 2].3 + 2 + 2 = 7.Example 2:
Input: nums = [4,2,8,3]
Output: 9
Explanation:
a = 0, b = 1, where nums[a] = 4 and nums[b] = 2. Since 4 % 2 == 0, replace nums[0] with nums[1].a = 2, b = 1, where nums[a] = 8 and nums[b] = 2. Since 8 % 2 == 0, replace nums[2] with nums[1].[2, 2, 2, 3].2 + 2 + 2 + 3 = 9.Example 3:
Input: nums = [7,5,9]
Output: 21
Explanation:
(a, b) such that nums[a] % nums[b] == 0.7 + 5 + 9 = 21.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105Problem Overview: You are given an integer array and allowed to repeatedly replace a number if it is divisible by another number in the array. The replacement typically reduces the value (for example replacing a number with the quotient after division). The goal is to perform these divisible replacements strategically so the final sum of the array is as small as possible.
Approach 1: Brute Force Pair Simulation (O(n^2 log n) time, O(1) space)
Check every pair (i, j) and see whether nums[i] % nums[j] == 0. If the condition holds, simulate replacing nums[i] with the smaller value produced by the division. After each modification, recompute possible pairs and continue until no further reduction is possible. This method literally simulates all valid operations and repeatedly scans the array to find reductions. It works for small inputs but becomes slow because every iteration requires checking all pairs again.
Approach 2: Greedy with Sorting (O(n log n) time, O(1) space)
Sort the array so smaller values appear first. Smaller numbers are more likely to divide larger numbers, which means they can create larger reductions in the total sum. Iterate from the smallest element and attempt to reduce larger elements whenever the divisibility condition holds. Sorting ensures that when you evaluate nums[i], all potential divisors before it are already minimal candidates. This greedy ordering significantly cuts down redundant checks compared to the brute force approach.
Approach 3: Greedy + Min Heap Reduction (O(n log n) time, O(n) space)
Push all values into a min heap. The smallest element becomes the most useful divisor because it can potentially reduce many larger elements. Repeatedly pop the smallest value and attempt to reduce other elements that are divisible by it. When a reduction occurs, push the new value back into the heap so it can participate in future operations. The heap structure ensures you always process the smallest divisor first, which tends to minimize the total sum quickly.
Recommended for interviews: The greedy strategy combined with sorting or a heap is what interviewers typically expect. Starting with the brute force pair check shows you understand the divisibility relationship between elements. Transitioning to a greedy strategy demonstrates algorithmic insight and reduces the complexity to roughly O(n log n). Concepts here overlap with greedy algorithms, heap / priority queue, and basic array processing.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Simulation | O(n^2 log n) | O(1) | Useful for understanding the operation mechanics or when n is very small |
| Greedy with Sorting | O(n log n) | O(1) | General case when divisibility relationships exist across many elements |
| Greedy with Min Heap | O(n log n) | O(n) | When repeated reductions occur and smallest values should be processed first |
Practice Minimize Array Sum Using Divisible Replacements with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor