Minimum Operations to Make Array Modulo Alternating II Problem
Description
LeetCode Problem 3944.
You are given an integer array nums and an integer k.
In one operation, you can increase or decrease any element of nums by 1.
An array is called modulo alternating if there exist two distinct integers x and y (0 <= x, y < k) such that:
- For every even index
i,nums[i] % k == x - For every odd index
i,nums[i] % k == y
Return the minimum number of operations required to make nums modulo alternating.
Example 1:
Input: nums = [1,4,2,8], k = 3
Output: 2
Explanation:
- Let's choose x = 1 for even indices and y = 2 for odd indices.
- Perform the following operations:
- Increment nums[1] = 4 by 1, giving nums = [1, 5, 2, 8].
- Decrement nums[2] = 2 by 1, giving nums = [1, 5, 1, 8].
- Now, for even indices, nums[i] % k = 1, and for odd indices, nums[i] % k = 2.
- Thus, the total number of operations required is 2.
Example 2:
Input: nums = [1,1,1], k = 3
Output: 1
Explanation:
- Incrementing nums[1] by 1 gives nums = [1, 2, 1], which satisfies the condition with x = 1 and y = 2.
- Thus, the total number of operations required is 1.
Example 3:
Input: nums = [6,7,8], k = 2
Output: 0
Explanation:
The array already satisfies the condition with x = 0 and y = 1. Thus, no operations are required.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^92 <= k <= 10^5
Solution
Approach: Circular Costs with a Difference Array
Taking each remainder modulo k, the cost of turning remainder v into a target t is the circular distance min(|t - v|, k - |t - v|). Even and odd indices are independent except for the rule x != y, so we compute two arrays: even[x], the total cost of making every even index reach x, and odd[y], the same for odd indices.
Computing even[x] for all x directly would take O(k2), so we compute it incrementally. First get cost[0] directly. Then, when x moves to x + 1, each remainder c changes the cost by +1 if x is within the next k / 2 steps after c (the target is moving away), by -1 once it is past the halfway point, and by 0 at exactly the halfway point when k is odd. These changes are ranges on a circle, so we add them to a difference array and take a running sum.
Finally, for each x, pair it with the best y != x. It is enough to remember the two smallest values of odd: use the best one unless its index equals x, and the second best then. The answer is the minimum of even[x] + odd[y]. Use 64-bit integers.
Complexity: O(n + k) time and O(k) space.
from math import inf
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
half = k // 2
def costs(vals: List[int]) -> List[int]:
# cost[x] = sum over vals of the circular distance between x and v % k
cnt = [0] * k
for v in vals:
cnt[v % k] += 1
cost = [0] * (k)
for c in range(k):
cost[0] += cnt[c] * min(c, k - c)
# step[x] = cost[x + 1] - cost[x]. A residue c adds +1 when x is in
# [c, c + half - 1] (circular), 0 at c + half when k is odd, else -1.
diff = [0] * (k + 1)
def add(l: int, length: int, w: int) -> None:
l %= k
r = l + length
diff[l] += w
if r <= k:
diff[r] -= w
else:
diff[k] -= w
diff[0] += w
diff[r - k] -= w
base = -len(vals)
for c in range(k):
w = cnt[c]
if w:
add(c, half, 2 * w)
if k % 2:
add(c + half, 1, w)
run = 0
for x in range(k - 1):
run += diff[x]
cost[x + 1] = cost[x] + base + run
return cost
even = costs(nums[0::2])
odd = costs(nums[1::2])
best = sorted(range(k), key=lambda y: odd[y])[:2]
ans = inf
for x in range(k):
y = best[0] if best[0] != x else best[1]
ans = min(ans, even[x] + odd[y])
return ans