Alternating Groups III - Problem

Imagine you're looking at a beautiful circular mosaic made of red and blue tiles. Your task is to analyze patterns in this circular arrangement and make dynamic changes to it!

You are given a circular array colors where each tile is either red (0) or blue (1). An alternating group is a contiguous sequence of tiles where adjacent tiles have different colors - like a perfect checkerboard pattern.

You need to handle two types of queries:

  • Type 1: [1, size] - Count how many alternating groups of the given size exist in the circle
  • Type 2: [2, index, color] - Change the tile at position index to the specified color

Return an array containing the results of all Type 1 queries in order. Remember, since the tiles form a circle, the first and last tiles are neighbors!

Input & Output

example_1.py โ€” Basic Alternating Pattern
$ Input: colors = [0,1,1,0,1], queries = [[2,1,0],[1,4],[1,3]]
โ€บ Output: [1, 2]
๐Ÿ’ก Note: Initially: [0,1,1,0,1]. After query [2,1,0]: [0,0,1,0,1]. Query [1,4] asks for groups of size 4 - there's 1 such group starting at index 1. Query [1,3] asks for groups of size 3 - there are 2 such groups.
example_2.py โ€” All Same Color
$ Input: colors = [0,0,0,0], queries = [[1,3],[2,0,1],[1,3]]
โ€บ Output: [0, 0]
๐Ÿ’ก Note: Initially all tiles are red [0,0,0,0], so no alternating groups exist. After changing index 0 to blue: [1,0,0,0], still no alternating groups of size 3 exist.
example_3.py โ€” Perfect Alternating Circle
$ Input: colors = [0,1,0,1], queries = [[1,2],[1,3],[1,4]]
โ€บ Output: [4, 4, 4]
๐Ÿ’ก Note: The circle [0,1,0,1] is perfectly alternating. There are 4 groups of size 2, 4 groups of size 3, and 4 groups of size 4 (since we can start at any position).

Constraints

  • 1 โ‰ค colors.length โ‰ค 5 ร— 104
  • 0 โ‰ค colors[i] โ‰ค 1
  • 1 โ‰ค queries.length โ‰ค 5 ร— 104
  • queries[i][0] โˆˆ {1, 2}
  • Type 1: 1 โ‰ค queries[i][1] โ‰ค colors.length
  • Type 2: 0 โ‰ค queries[i][1] < colors.length, 0 โ‰ค queries[i][2] โ‰ค 1

Visualization

Tap to expand
RBRBRBQuery ProcessingType 1: Count groups of size kType 2: Update color at indexResult: [counts for Type 1]Time: O(Q log N) optimalAlternating Group Example
Understanding the Visualization
1
Build Initial State
Analyze the circular array to identify alternating segments
2
Handle Type 1 Queries
Count alternating groups of the specified size using segment information
3
Handle Type 2 Updates
Update colors and efficiently recompute affected segments
4
Maintain Circular Property
Ensure the first and last elements are treated as neighbors
Key Takeaway
๐ŸŽฏ Key Insight: By tracking alternating segments instead of checking every position, we can efficiently handle both dynamic updates and range queries in logarithmic time.
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 15
24.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen