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:
i, nums[i] % k == xi, nums[i] % k == yReturn the minimum number of operations required to make nums modulo alternating.
Example 1:
Input: nums = [1,4,2,8], k = 3
Output: 2
Explanation:
x = 1 for even indices and y = 2 for odd indices.nums[1] = 4 by 1, giving nums = [1, 5, 2, 8].nums[2] = 2 by 1, giving nums = [1, 5, 1, 8].nums[i] % k = 1, and for odd indices, nums[i] % k = 2.Example 2:
Input: nums = [1,1,1], k = 3
Output: 1
Explanation:
nums[1] by 1 gives nums = [1, 2, 1], which satisfies the condition with x = 1 and y = 2.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 1092 <= k <= 100Problem Overview: You are given an integer array and need the minimum number of operations required so that the array becomes modulo alternating. After applying modulo (typically mod 2), adjacent elements must produce alternating remainders such as 0,1,0,1... or 1,0,1,0.... Each operation modifies an element so its remainder matches the required alternating pattern.
Approach 1: Brute Force Pattern Simulation (O(n), O(1))
There are only two valid alternating remainder patterns for modulo 2: starting with 0 (0,1,0,1...) or starting with 1 (1,0,1,0...). Iterate through the array and compute nums[i] % 2. Compare this remainder with the expected value for both patterns. Count mismatches separately for each pattern because every mismatch represents one required operation. The answer is the minimum mismatch count between the two patterns. The scan is linear, so the time complexity is O(n) and only a few counters are used, giving O(1) space.
Approach 2: Greedy Parity Tracking (O(n), O(1))
Instead of explicitly building both patterns, track the expected parity while iterating. For pattern A, expect i % 2 to match the remainder; for pattern B, expect (i + 1) % 2. For each element compute nums[i] % 2 and increment the mismatch counter when it differs from the expected parity. This greedy counting works because each index is independent—changing one element does not affect other modulo results. Complexity remains O(n) time and O(1) space.
Approach 3: Frequency-Based Counting (O(n), O(1))
You can also separate the array into even and odd index groups. Count how many elements in each group already produce remainder 0 or 1. From these counts, compute how many modifications are needed to enforce each alternating configuration. This approach reframes the problem as a counting task and can be easier to reason about in interviews when discussing parity distribution. It still runs in O(n) time and uses constant extra memory.
Recommended for interviews: The greedy parity counting approach is what most interviewers expect. It demonstrates recognition that only two alternating modulo patterns exist and that the optimal solution is simply the minimum mismatch count from a single pass. The brute-force comparison shows correct reasoning, while the optimized scan highlights strong understanding of arrays and greedy algorithms. Many candidates also mention parity handling through modular arithmetic, which clarifies why the problem reduces to checking nums[i] % 2.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pattern Simulation | O(n) | O(1) | When validating both alternating modulo patterns directly for clarity |
| Greedy Parity Tracking | O(n) | O(1) | Best general solution; single pass and minimal logic |
| Frequency-Based Counting | O(n) | O(1) | Useful when reasoning about even/odd index groups and remainder frequencies |
Practice Minimum Operations to Make Array Modulo Alternating I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor