Watch 10 video solutions for Alternating Groups II, a medium level problem involving Array, Sliding Window. This walkthrough by NeetCodeIO has 11,774 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 and an integer k. 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.An alternating group is every k contiguous tiles in the circle with alternating colors (each tile in the group except the first and last one has a different color from its left and right tiles).
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 = [0,1,0,1,0], k = 3
Output: 3
Explanation:

Alternating groups:



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

Alternating groups:


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

Constraints:
3 <= colors.length <= 1050 <= colors[i] <= 13 <= k <= colors.lengthProblem Overview: You are given a circular array colors and an integer k. A group is valid if the k consecutive tiles form an alternating pattern, meaning every adjacent pair has different colors. Because the array is circular, groups may wrap from the end back to the start.
Approach 1: Sliding Window on Alternation Count (O(n) time, O(1) space)
This approach treats the problem as checking whether each window of size k maintains alternating neighbors. For a group of length k, there must be exactly k-1 alternating edges. Iterate through the array while maintaining a sliding window that tracks whether adjacent elements differ. Since the array is circular, iterate up to n + k positions or use modulo indexing. Each step updates the number of valid alternating edges in the current window; when the count reaches k-1, you found a valid group. This technique is common in sliding window problems where the window property can be updated incrementally instead of recomputed.
Approach 2: Pattern Recognition with Auxiliary Array (O(n) time, O(n) space)
Another way to think about the problem is to precompute whether each adjacent pair alternates. Build an auxiliary array alt where alt[i] = 1 if colors[i] != colors[(i+1) % n], otherwise 0. A valid group of size k requires k-1 consecutive ones in this array. After building the array, run a sliding window of size k-1 across the circular structure and count windows whose sum equals k-1. This separates pattern detection from counting, which makes the logic easier to reason about when working with array transformations and pattern scanning.
The key insight across both solutions is that alternating groups are defined by edges between elements, not the elements themselves. Once you track whether neighboring values differ, the problem reduces to counting stretches of valid edges. The circular nature only affects indexing, which you handle using modulo operations or by extending the traversal range.
Recommended for interviews: The sliding window approach is the expected solution. It runs in O(n) time with constant extra space and demonstrates strong understanding of window maintenance and circular traversal. The auxiliary-array method is also linear time but uses extra memory; it’s easier to reason about and can help you derive the optimal solution during an interview.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window on Alternation Count | O(n) | O(1) | Best general solution. Efficient for large arrays and commonly expected in interviews. |
| Auxiliary Alternation Array + Sliding Window | O(n) | O(n) | Useful when separating pattern detection from counting improves clarity or debugging. |