Maximum Score with Co-Prime Element Problem
Description
LeetCode Problem 3953.
You are given an integer array nums of length n and an integer maxVal.
You may change any element in nums to any positive integer less than or equal to maxVal. Each such change costs 1.
Two integers are co-prime if their greatest common divisor (GCD) is 1.
After all modifications, you must choose an index i such that, nums[i] is co-prime with every other element nums[j].
Let:
selectedValuebe the final value ofnums[i]after modifications.modificationCostbe the total number of elements changed.
The score is defined as score = selectedValue - modificationCost.
Return the maximum possible score.
Example 1:
Input: nums = [3,4,6], maxVal = 5
Output: 4
Explanation:
Change nums[2] from 6 to 5, which costs 1. Choose nums[2] = 5, since it is co-prime with 3 and 4.
- selectedValue = 5
- modificationCost = 1
- The score is 5 - 1 = 4
Example 2:
Input: nums = [1,2,3], maxVal = 4
Output: 3
Explanation:
No modifications are required. Choose nums[2] = 3, since it is co-prime with 1 and 2.
- selectedValue = 3
- modificationCost = 0
- The score is 3 - 0 = 3
Example 3:
Input: nums = [2,2], maxVal = 1
Output: 1
Explanation:
Change nums[0] from 2 to 1, which costs 1. Choose nums[1] = 2, since it is co-prime with 1.
- selectedValue = 2
- modificationCost = 1
- The score is 2 - 1 = 1
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^51 <= maxVal <= 10^5
Solution
Approach: Möbius Inversion
Fix the final value v of the chosen element. Every other element not co-prime with v must be changed (to 1, say), at cost 1 each. Let bad(v) be how many elements have gcd(x, v) > 1.
- If
valready appears innums, pick that element for free: the cost ismax(bad(v) - 1, 0), because it counts itself whenv > 1. This works even whenv > maxVal. - Otherwise
vmust be at mostmaxVal, and we change one element intov. If some element is not co-prime withv, change one of those, so the cost isbad(v); if none is, the cost is 1. Either way it ismax(bad(v), 1).
The answer is the best v - cost over all v up to max(maxVal, max(nums)).
To get bad(v) for every v, count the elements co-prime with v as the sum over divisors d of v of mu(d) * cnt[d], where cnt[d] is how many elements are divisible by d and mu is the Möbius function. A sieve builds mu, and looping over multiples fills everything in harmonic-sum time.
Complexity: time O(M log M), space O(M), where M = max(maxVal, max(nums)).
class Solution:
def maxScore(self, nums: List[int], maxVal: int) -> int:
m = max(maxVal, max(nums))
freq = [0] * (m + 1)
for x in nums:
freq[x] += 1
# Moebius function
mu = [1] * (m + 1)
is_comp = [False] * (m + 1)
primes = []
for i in range(2, m + 1):
if not is_comp[i]:
primes.append(i)
mu[i] = -1
for p in primes:
if i * p > m:
break
is_comp[i * p] = True
if i % p == 0:
mu[i * p] = 0
break
mu[i * p] = -mu[i]
# coprime[v] = number of elements coprime with v
coprime = [0] * (m + 1)
for d in range(1, m + 1):
if mu[d] == 0:
continue
cnt = sum(freq[d::d])
if cnt == 0:
continue
for v in range(d, m + 1, d):
coprime[v] += mu[d] * cnt
n = len(nums)
ans = 0
for v in range(1, m + 1):
bad = n - coprime[v]
if freq[v]:
cost = max(bad - 1, 0)
elif v <= maxVal:
cost = max(bad, 1)
else:
continue
ans = max(ans, v - cost)
return ans