Watch 5 video solutions for Alternating Groups I, a easy level problem involving Array, Sliding Window. This walkthrough by Aryan Mittal has 6,390 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There is a circle of red and blue tiles. You are given an array of integers colors. The color of tile i is represented by colors[i]:
colors[i] == 0 means that tile i is red.colors[i] == 1 means that tile i is blue.Every 3 contiguous tiles in the circle with alternating colors (the middle tile has a different color from its left and right tiles) is called an alternating group.
Return the number of alternating groups.
Note that since colors represents a circle, the first and the last tiles are considered to be next to each other.
Example 1:
Input: colors = [1,1,1]
Output: 0
Explanation:

Example 2:
Input: colors = [0,1,0,0,1]
Output: 3
Explanation:

Alternating groups:



Constraints:
3 <= colors.length <= 1000 <= colors[i] <= 1Problem Overview: You are given a circular colors array where each value represents a tile color. A group is valid if three consecutive tiles form an alternating pattern (for example 0,1,0 or 1,0,1). Because the array is circular, the sequence can wrap from the end back to the start. The task is to count how many length-3 groups satisfy this alternating rule.
Approach 1: Iterative Pattern Check (O(n) time, O(1) space)
Iterate through every index i and examine the next two elements using circular indexing. A valid alternating group occurs when colors[i] != colors[i+1] and colors[i+1] != colors[i+2]. These two comparisons guarantee the pattern flips at each step. Use modulo arithmetic to wrap indices: (i + 1) % n and (i + 2) % n. This approach simply scans the array once, performs constant comparisons per index, and increments a counter when the condition holds. The logic is straightforward and works well when the group size is fixed.
This technique relies purely on sequential iteration over the array. Since only three values are inspected at a time, memory usage stays constant and the runtime grows linearly with the number of tiles.
Approach 2: Sliding Window (O(n) time, O(1) space)
A more general way to reason about the problem is with a fixed-size sliding window of length 3. Start with the first three elements and verify whether they alternate. Then slide the window forward by one position at a time while maintaining circular indexing. Each shift removes the leftmost element and includes the next element in the circle.
The key insight is that the window validity depends only on two adjacent comparisons: a != b and b != c. Every time the window moves, recompute these comparisons for the new triplet. Because the window size never changes, each step performs constant work. The circular property is handled by indexing with modulo so the window naturally wraps from the last element to the first.
This method is conceptually useful because it mirrors how many larger alternating-pattern problems are solved. When group sizes become variable or constraints increase, the sliding window pattern scales better than hardcoding comparisons.
Recommended for interviews: The sliding window explanation is typically preferred. Interviewers expect you to recognize the alternating constraint as a local window property and scan the array in O(n) time with constant memory. The direct iteration solution also runs in linear time and clearly demonstrates understanding, but framing it as a window problem shows stronger familiarity with common algorithm patterns.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Pattern Check | O(n) | O(1) | Best when the group size is fixed (3) and you want the simplest implementation. |
| Sliding Window | O(n) | O(1) | Preferred conceptual approach for pattern detection problems and scalable to larger window sizes. |