Skip to main content

Minimum Operations to Make Array Modulo Alternating I - Solution & Explanation

MediumArrayEnumeration8 min read
Practice this problem

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 = 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.

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 109
  • 2 <= 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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pattern SimulationO(n)O(1)When validating both alternating modulo patterns directly for clarity
Greedy Parity TrackingO(n)O(1)Best general solution; single pass and minimal logic
Frequency-Based CountingO(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?
The problem is categorized as Medium because recognizing the two possible alternating modulo patterns is the key insight. Once identified, the implementation is straightforward with a linear scan and constant space.
Minimum Operations to Make Array Modulo Alternating I Python/Java solution
Implement a single pass through the array. Compute nums[i] % 2 and compare it with the expected parity for two possible alternating sequences. Maintain two mismatch counters and return the smaller value. The same logic works in Python, Java, C++, or any language with constant memory.
How to solve Minimum Operations to Make Array Modulo Alternating I in O(n)?
Iterate through the array and compute nums[i] % 2. Track mismatches for two patterns: expected parity i % 2 and expected parity (i + 1) % 2. Each mismatch indicates one operation needed to adjust that element's remainder. Return the minimum mismatch count after the scan.
What is the best approach for Minimum Operations to Make Array Modulo Alternating I?
The optimal approach checks two possible modulo patterns: 0,1,0,1... and 1,0,1,0.... Compute nums[i] % 2 for each element and count mismatches for both patterns during a single pass. The minimum mismatch count equals the minimum number of operations. This runs in O(n) time with O(1) extra space.
Is Minimum Operations to Make Array Modulo Alternating I asked at Google/Amazon/Meta?
Problems based on alternating patterns, parity checks, and modulo operations appear frequently in interviews at large tech companies. Variants of this problem test array traversal, greedy reasoning, and modular arithmetic, which are common topics in Google and Amazon interview question sets.
What data structure is used in Minimum Operations to Make Array Modulo Alternating I?
The problem primarily uses a simple array traversal with counters. No complex data structures are required—only integer counters to track mismatches for the two alternating modulo patterns.
What is the time complexity of Minimum Operations to Make Array Modulo Alternating I?
The optimal solution runs in O(n) time because the array is scanned once while comparing remainders against two expected alternating patterns. Only constant counters are maintained, so the space complexity is O(1).

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 FleetCode