Alternating Groups III - Problem

There are some red and blue tiles arranged in a circle. You are given an array of integers colors and a 2D array of integers queries.

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 a contiguous subset of tiles in the circle with alternating colors (each tile in the group except the first and last one has a different color from its adjacent tiles in the group).

You have to process queries of two types:

  • queries[i] = [1, size_i]: determine the count of alternating groups with size size_i
  • queries[i] = [2, index_i, color_i]: change colors[index_i] to color_i

Return an array answer containing the results of the queries of the first type in order.

Note: Since colors represents a circle, the first and the last tiles are considered to be next to each other.

Input & Output

Example 1 — Basic Pattern Counting
$ Input: colors = [0,1,1,0,1], queries = [[1,3],[2,1,0],[1,3]]
Output: [3,3]
💡 Note: First query [1,3] counts alternating groups of size 3. Groups starting at positions 2: [1,0,1], position 3: [0,1,0], and position 4: [1,0,0] are valid, giving 3 total. After updating index 1 to 0, colors becomes [0,0,1,0,1]. Groups of size 3 starting at positions 1: [0,1,0], position 2: [1,0,1], and position 3: [0,1,0] are valid, giving 3 total.
Example 2 — Size 1 Groups
$ Input: colors = [0,0,1,0], queries = [[1,4],[2,0,1],[1,4]]
Output: [1,0]
💡 Note: Initially only one alternating group of size 4 exists. After changing colors[0] to 1, no alternating groups of size 4 remain.
Example 3 — All Same Colors
$ Input: colors = [0,0,0], queries = [[1,2]]
Output: [0]
💡 Note: No alternating groups of size 2 exist when all colors are the same.

Constraints

  • 1 ≤ colors.length ≤ 5 × 104
  • 0 ≤ colors[i] ≤ 1
  • 1 ≤ queries.length ≤ 5 × 104
  • queries[i][0] ∈ {1, 2}
  • For type 1 queries: 1 ≤ size_i ≤ colors.length
  • For type 2 queries: 0 ≤ index_i < colors.length, 0 ≤ color_i ≤ 1

Visualization

Tap to expand
Alternating Groups III - Segment Tracking INPUT Colors Array (Circular) 0 1 1 0 1 i=0 i=1 i=2 i=3 i=4 colors = [0,1,1,0,1] Queries: [1,3] - Count size 3 groups [2,1,0] - Set colors[1]=0 [1,3] - Count size 3 groups Red (0) Blue (1) ALGORITHM STEPS 1 Track Break Points Find where colors[i]==colors[i+1] Breaks at: i=1 (both blue) 2 Store Segment Lengths Track alternating segments Seg: [4,0,1] len=4, [1,2] len=1 3 Query Type 1: Count For each segment of len L: Add max(0, L-size+1) groups Query [1,3]: size=3 Seg len 4: 4-3+1 = 2 groups Seg len 1: 1-3+1 = 0 groups Total: 2 4 Query Type 2: Update [2,1,0]: colors[1] = 0 New: [0,0,1,0,1] Breaks at i=0,1 now After update, Query [1,3]: Segments: len 3 [2,3,4,0] 3-3+1 = 2 (wraps around) FINAL RESULT Query 1: [1,3] Initial: [0,1,1,0,1] wrap Groups: [4,0,1], [0,1,wrap4] Count: 2 Update: [2,1,0] New: [0,0,1,0,1] wrap Query 2: [1,3] Groups: [2,3,4], [3,4,wrap0] Count: 2 OUTPUT [2, 2] OK - Both queries return 2 Key Insight: Use segment tracking with a data structure (like BIT/Fenwick tree) to efficiently track alternating segments. Break points occur where colors[i] == colors[(i+1) % n]. Each segment contributes max(0, length - size + 1) groups. Updates only affect break points at index-1, index, and index+1, allowing O(log n) updates. TutorialsPoint - Alternating Groups III | Segment Tracking Approach
Asked in
Google 15 Meta 12 Amazon 8
3.2K Views
Medium Frequency
~35 min Avg. Time
145 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