Skip to main content
LeetCode 3208, Medium. Topics: Array, Sliding Window. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

Problem

There is a circle of red and blue tiles. You are given an array of integers <code>colors</code> and an integer <code>k</code>. The color of tile <code>i</code> is represented by <code>colors[i]</code>: <ul> <li><code>colors[i] == 0</code> means that tile <code>i</code> is <strong>red</strong>.</li> <li><code>colors[i] == 1</code> means that tile <code>i</code> is <strong>blue</strong>.</li> </ul> <p>An <strong>alternating</strong> group is every <code>k</code> contiguous tiles in the circle with <strong>alternating</strong> colors (each tile in the group except the first and last one has a different color from its <strong>left</strong> and <strong>right</strong> tiles).</p> <p>Return the number of <strong>alternating</strong> groups.</p> <p><strong>Note</strong> that since <code>colors</code> represents a <strong>circle</strong>, the <strong>first</strong> and the <strong>last</strong> tiles are considered to be next to each other.</p>

Examples

Example 1
Alternating groups: Group 1 Group 2 Group 3 Example 2
Alternating groups: Group 1 Group 2 Example 3

Constraints

  • 3 <= colors.length <= 10^5
  • 0 <= colors[i] <= 1
  • 3 <= k <= colors.length

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026