There are some red and blue tiles arranged circularly. You are given an array of integers colors and a 2D integers array 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, sizei], determine the count of alternating groups with size sizei.queries[i] = [2, indexi, colori], change colors[indexi] to colori.Return an array answer containing the results of the queries of the first type in order.
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,1,0,1], queries = [[2,1,0],[1,4]]
Output: [2]
Explanation:

First query:
Change colors[1] to 0.

Second query:
Count of the alternating groups with size 4:


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

First query:
Count of the alternating groups with size 3:


Second query: colors will not change.
Third query: There is no alternating group with size 5.
Constraints:
4 <= colors.length <= 5 * 1040 <= colors[i] <= 11 <= queries.length <= 5 * 104queries[i][0] == 1 or queries[i][0] == 2i that:
queries[i][0] == 1: queries[i].length == 2, 3 <= queries[i][1] <= colors.length - 1queries[i][0] == 2: queries[i].length == 3, 0 <= queries[i][1] <= colors.length - 1, 0 <= queries[i][2] <= 1#3245 Alternating Groups III extends the alternating pattern problem by introducing dynamic updates and queries. The key idea is to treat the array as a circular structure and track segments where adjacent elements alternate. Whenever an element changes, only its neighboring relationships are affected, which allows localized updates instead of recomputing the entire structure.
A common strategy is to maintain boundaries of alternating segments and track their lengths. A Binary Indexed Tree (Fenwick Tree) can be used to store counts or frequencies of segment lengths so queries about valid alternating groups can be answered efficiently. When an update occurs, affected segments may split or merge, and the BIT is updated accordingly.
This design supports fast updates and queries by leveraging prefix sums over segment lengths. The overall complexity typically reaches O((n + q) log n), making it suitable for large inputs where naive recomputation would be too slow.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Segment tracking with Binary Indexed Tree | O((n + q) log n) | O(n) |
| Naive recomputation after each query | O(n * q) | O(1) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
Try using a segment tree to store the maximal alternating groups.
Store the sizes of these maximal alternating groups in another data structure.
Find the count of the alternating groups of size <code>k</code> with having the count of maximal alternating groups with size greater than or equal to <code>k</code> and the sum of their sizes.
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Problems involving dynamic array updates and Fenwick Trees frequently appear in FAANG-style interviews. While this exact question may vary, the underlying concepts of segment maintenance and BIT usage are commonly tested.
A Binary Indexed Tree (Fenwick Tree) works well because it supports efficient prefix sums and frequency counting. It helps track how many alternating segments of certain lengths exist after updates.
The optimal approach tracks alternating segments and updates them efficiently when elements change. Using a Binary Indexed Tree allows fast aggregation of segment lengths, enabling queries to be answered in logarithmic time.
The challenge comes from handling dynamic updates while maintaining information about alternating segments in a circular array. Efficiently merging and splitting segments while answering queries requires advanced data structures.