Skip to main content

Minimize Array Sum Using Divisible Replacements - Video Solutions

MediumArrayHash TableMathGreedyNumber Theory

Minimize Array Sum Using Divisible Replacements | Leetcode 3927

Techdose
24:47755 views
8 video solutions available

Minimize Array Sum Using Divisible Replacements - Video Solution

Watch 8 video solutions for Minimize Array Sum Using Divisible Replacements, a medium level problem involving Array, Hash Table, Math. This walkthrough by Techdose has 755 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples

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.

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