Ezvor

3944. Minimum Operations to Make Array Modulo Alternating II

HardArray Preparing judge… LeetCode
ArrayEnumeration

Hints

Hint 1
For a number with remainder r = nums[i] % k, the minimum cost to change it to remainder t is: min(abs(r - t), k - abs(r - t))
Hint 2
Separate the indices by parity. Compute evenCost[x] = total cost to make all even indices have remainder x, oddCost[y] = total cost to make all odd indices have remainder y
Hint 3
Then the answer is: min(evenCost[x] + oddCost[y]) over all x != y.
Hint 4
To avoid O(k2), keep the smallest and second smallest values in oddCost. For each x, use the best odd remainder unless it equals x, otherwise use the second best.