Minimum Operations to Make Array Equal III Problem
Description
LeetCode Problem 4005.
You are given an integer array nums.
In one operation, you may choose any element nums[i] and perform one of the following:
- Multiply
nums[i]by an integerk, wherek >= 2. - Divide
nums[i]by an integerk, where2 <= k < nums[i], provided thatnums[i]is divisible byk.
Return the minimum number of operations required to make all elements of nums equal.
Example 1:
Input: nums = [6,12,8]
Output: 3
Explanation:
We can perform following operates to make all numbers to 6:
- Divide nums[1] = 12 by 2 to get 6.
- Divide nums[2] = 8 by 4 to get 2.
- Multiply nums[2] = 2 by 3 to get 6.
Example 2:
Input: nums = [5,15,20]
Output: 2
Explanation:
We can perform following operates to make all numbers to 5:
- Divide nums[1] = 15 by 3 to get 5.
- Divide nums[2] = 20 by 4 to get 5.
Example 3:
Input: nums = [7,7,7]
Output: 0
Explanation:
All elements are already equal, so no operations are needed.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^9
Solution
Approach: Divisibility Counting
First look at the cost of turning one number a into a target T >= 2:
0ifa == T;1ifadividesT(multiply byT / a) orTdividesa(divide bya / T);2otherwise (for example multiplyaup to a common multiple, then divide down toT).
The value 1 can never be the target unless every number is already 1, because a division can never produce 1.
Count for a target T the numbers that divide T (down) and the numbers that are multiples of T (up); a number equal to T is counted in both. The total cost is 2n - down - up.
Which targets are worth trying? If T is not in nums, at most all n numbers can be comparable with it, and the least common multiple of all numbers already reaches n. So the candidates are the numbers in nums plus that "gain n" option, and the answer is 2n - max(n, best), where best is the largest down + up over the values in nums that are at least 2.
To compute down and up for every distinct value a, enumerate its divisors from its prime factorization (primes up to sqrt(10^9) from a sieve). For each divisor d that also occurs in nums, add count[d] to down[a] and count[a] to up[d].
Complexity: time O(m * (sqrt(V) / ln V + D)) and space O(m), where m is the number of distinct values, V = 10^9 and D <= 1344 is the maximum number of divisors.
class Solution:
def minOperations(self, nums: List[int]) -> int:
n = len(nums)
cnt = Counter(nums)
if cnt[1] == n:
return 0
primes = []
sieve = [True] * 31700
for i in range(2, 31700):
if sieve[i]:
primes.append(i)
for j in range(i * i, 31700, i):
sieve[j] = False
down = dict.fromkeys(cnt, 0) # numbers in nums that divide v
up = dict.fromkeys(cnt, 0) # numbers in nums that are multiples of v
for a in cnt:
divs = [1]
x = a
for p in primes:
if p * p > x:
break
if x % p == 0:
e = 0
while x % p == 0:
x //= p
e += 1
divs = [d * p**k for d in divs for k in range(e + 1)]
if x > 1:
divs += [d * x for d in divs]
for d in divs:
if d in cnt:
down[a] += cnt[d]
up[d] += cnt[a]
best = n
for v in cnt:
if v > 1:
best = max(best, down[v] + up[v])
return 2 * n - best