Imagine there is a circle of red and blue tiles. The color of the tiles are represented by the array tileColors, where tileColors[i] = 0 means that the ith tile is red, whereas tileColors[i] = 1 means that the ith tile is blue.
We want to determine whether the tiles that are next to each other in the circle have alternating colors. The ith tile should have a different color than both the i+1th and the i-1th neighboring tiles. Given an integer size, determine how many groups of size consecutive tiles have alternating colors.
Note: Because tileColors represents a circle, the first and last tiles are considered to be next to each other.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(tileColors.length2) will fit within the execution time limit.
tileColors = [0, 1, 0, 1, 1] size = 3 return = 3
There are five unique groups of size 3:
tileColors[0...2] = [0, 1, 0], which has alternating colors.tileColors[1...3] = [1, 0, 1], which has alternating colors.tileColors[2...4] = [0, 1, 1], which does not have alternating colors.tileColors[3...4] + tileColors[0] = [1, 1, 0], which does not have alternating colors.tileColors[4] + tileColors[0...1] = [1, 0, 1], which has alternating colors.
There are 3 groups that have alternating colors.
- Check Even-Position MonotonicityOA · Seen Jun 2026
- Count Even-Digit NumbersOA · Seen Jun 2026
- Inventory Discount TrackerOA · Seen Jun 2026
- Construct WDL StringOA · Seen Jun 2026
- Find Sum PairsOA · Seen Jun 2026
- LRU Cache with TTL ExpirationONSITE INTERVIEW · Seen May 2026
- Maximum Candies with At Most Two Types in a LineONSITE INTERVIEW · Seen May 2026
- Top-K KOLs by Total LikesONSITE INTERVIEW · Seen May 2026
public int solution(int[] tileColors, int size) {
// write your code here
}