The Indian Engineer

Problem 3208 Alternating Groups II

Posted on 5 mins

Array Sliding Window

Problem Statement

Link - Problem 3208

Question

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]:

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:

Solution

class Solution {
public:
    int numberOfAlternatingGroups(vector<int>& colors, int k) {
        for (int i = 0; i < k - 1; ++i)
            colors.push_back(colors[i]);
        int res = 0;
        int cnt = 1;
        for (int i = 1; i < colors.size(); ++i) {
            if (colors[i] != colors[i - 1])
                ++cnt;
            else
                cnt = 1;

            if (cnt >= k)
                ++res;
        }
        return res;
    }
};

Complexity Analysis

| Algorithm                                      | Time Complexity | Space Complexity |
| ---------------------------------------------- | --------------- | ---------------- |
| Modded Sliding Window Enumeration and Counting | O(n)            | O(k)             |

Explanation

Intial Thoughts

To approach this problem, first understand that we have a circular arrangement of red and blue tiles. An alternating group is formed when k contiguous tiles have alternating colors. Since the arrangement is circular, we need to consider the last and first tiles as adjacent. Initially, we cannot directly analyze the given array because of its circular nature. We need to consider the given array as a non-circular array by either making a copy or adding the first elements again at the end of the array to make it look like the circle. Then, we can iterate through the array to check for alternating groups.

Intuitive Analysis

An intuitive way to solve this problem is to think of it as identifying a pattern in the array. To identify an alternating pattern, we can simply check for adjacent tiles with different colors. We can maintain a counter to keep track of the number of continuous alternating tiles we have seen so far. As soon as we encounter a tile with the same color as its previous one, we can reset the counter. We can then check if the counter is equal to or greater than k. If yes, we have identified an alternating group and increment our result.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Try to find a tile that has the same color as its next tile (if it exists).

Then try to find maximal alternating groups by starting a single for loop from that tile.