3927. Minimize Array Sum Using Divisible Replacements
Description
You are given an integer array nums.
You can perform the following operation any number of times:
- Choose two indices
aandbsuch thatnums[a] % nums[b] == 0. - Replace
nums[a]withnums[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, wherenums[a] = 6andnums[b] = 2. Since6 % 2 == 0, replacenums[1]withnums[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, wherenums[a] = 4andnums[b] = 2. Since4 % 2 == 0, replacenums[0]withnums[1]. - Choose
a = 2,b = 1, wherenums[a] = 8andnums[b] = 2. Since8 % 2 == 0, replacenums[2]withnums[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 thatnums[a] % nums[b] == 0. - Hence, no operation can be performed. The sum remains
7 + 5 + 9 = 21.
Β
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 10βββββββ5
Solutions
Solution 1
1 | |
1 | |
1 | |
1 | |