Problem · Array

Count Alternating Tile Groups

EasyTiktokOA
See Tiktok hiring insights

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.

Examples
01 · Example 1
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.

More Tiktok problems
drafts saved locally
public int solution(int[] tileColors, int size) {
  // write your code here
}
tileColors[0, 1, 0, 1, 1]
size3
expected3
sign in to submit