Skip to main content

Minimize Array Sum Using Divisible Replacements - Solution & Explanation

MediumArrayHash TableMathGreedy4 min readAsked at: Amazon
Practice this problem

Problem Statement

You are given an integer array nums.

You can perform the following operation any number of times:

  • Choose two indices a and b such that nums[a] % nums[b] == 0.
  • Replace 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:

  • Choose a = 1, b = 2, where nums[a] = 6 and nums[b] = 2. Since 6 % 2 == 0, replace nums[1] with nums[2].
  • The array becomes [3, 2, 2].
  • No further operation reduces the sum. Thus, the final sum is 3 + 2 + 2 = 7.

Example 2:

Input: nums = [4,2,8,3]

Output: 9

Explanation:

  • Choose a = 0, b = 1, where nums[a] = 4 and nums[b] = 2. Since 4 % 2 == 0, replace nums[0] with nums[1].
  • Choose a = 2, b = 1, where nums[a] = 8 and nums[b] = 2. Since 8 % 2 == 0, replace nums[2] with nums[1].
  • The array becomes [2, 2, 2, 3].
  • No further operation reduces the sum. Thus, the final sum is 2 + 2 + 2 + 3 = 9.

Example 3:

Input: nums = [7,5,9]

Output: 21

Explanation:

  • There is no pair (a, b) such that nums[a] % nums[b] == 0.
  • Hence, no operation can be performed. The sum remains 7 + 5 + 9 = 21.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 10​​​​​​​5

Approach Overview

Problem 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

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pair SimulationO(n^2 log n)O(1)Useful for understanding the operation mechanics or when n is very small
Greedy with SortingO(n log n)O(1)General case when divisibility relationships exist across many elements
Greedy with Min HeapO(n log n)O(n)When repeated reductions occur and smallest values should be processed first

Video Solution

Minimize Array Sum Using Divisible Replacements | Leetcode 3927Techdose755 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Minimize Array Sum Using Divisible Replacements easy or hard?
The problem is generally categorized as Medium difficulty. The challenge comes from recognizing that greedy ordering of divisors leads to the minimal sum, rather than attempting every possible pair operation.
Minimize Array Sum Using Divisible Replacements Python/Java solution
Most solutions implement a greedy strategy. Sort the array or use a min heap, iterate through possible divisors, and reduce numbers that are divisible. Python commonly uses heapq while Java implementations use PriorityQueue.
How to solve Minimize Array Sum Using Divisible Replacements in O(n log n)?
Sort the array or push values into a min heap so the smallest numbers are processed first. For each small value, attempt to reduce larger values that are divisible by it. Greedy reductions ensure the sum decreases quickly while maintaining O(n log n) complexity from sorting or heap operations.
What is the best approach for Minimize Array Sum Using Divisible Replacements?
The most practical approach uses a greedy strategy combined with sorting or a min heap. Process smaller numbers first because they can divide larger numbers and create bigger reductions in value. This reduces unnecessary checks and brings the complexity down to about O(n log n).
Is Minimize Array Sum Using Divisible Replacements asked at Google/Amazon/Meta?
Problems involving divisibility transformations and greedy reductions appear in interviews at companies like Amazon and Google. While the exact problem number may vary, the underlying ideas—greedy decisions, heap usage, and divisibility checks—are common interview patterns.
What data structure is used in Minimize Array Sum Using Divisible Replacements?
Typical implementations rely on arrays for iteration and optionally a priority queue (min heap) to always process the smallest value first. The heap helps efficiently retrieve divisors that can reduce larger numbers.
What is the time complexity of Minimize Array Sum Using Divisible Replacements?
The optimal greedy solution runs in O(n log n) time due to sorting or heap operations. The brute force simulation that checks every pair of elements can take O(n^2) or worse depending on how many replacement operations occur.

Ready to solve this problem?

Practice Minimize Array Sum Using Divisible Replacements with our built-in code editor and test cases.

Practice on FleetCode