Minimum Operations to Make Array Modulo Alternating I - Solution & Explanation
Problem Statement
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 = 1for even indices andy = 2for odd indices. - Perform the following operations:
- Increment
nums[1] = 4by 1, givingnums = [1, 5, 2, 8]. - Decrement
nums[2] = 2by 1, givingnums = [1, 5, 1, 8].
- Increment
- 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 givesnums = [1, 2, 1], which satisfies the condition withx = 1andy = 2. - Thus, the total number of operations required is 1.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 1092 <= k <= 100
Approach Overview
Problem 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.
Solution
We can enumerate the target value x for even indices and the target value y for odd indices, where 0 leq x, y < k and x neq y. For each element, we calculate the number of operations required to change it to the target value, and accumulate the total number of operations. Finally, we return the minimum value among all enumeration results.
The time complexity is O(n times k^2), where n is the length of the array nums. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| 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 |
Video Solution
Biweekly - 183 | Q2. Minimum Operations to Make Array Modulo Alternating I | LeetCode • aniketdevlp • 252 views views
Watch 2 more video solutions →Frequently Asked Questions
Is Minimum Operations to Make Array Modulo Alternating I easy or hard?
Minimum Operations to Make Array Modulo Alternating I Python/Java solution
How to solve Minimum Operations to Make Array Modulo Alternating I in O(n)?
What is the best approach for Minimum Operations to Make Array Modulo Alternating I?
Is Minimum Operations to Make Array Modulo Alternating I asked at Google/Amazon/Meta?
What data structure is used in Minimum Operations to Make Array Modulo Alternating I?
What is the time complexity of Minimum Operations to Make Array Modulo Alternating I?
Ready to solve this problem?
Practice Minimum Operations to Make Array Modulo Alternating I with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor