FastPrepCount Circular Alternating Binary Windows

Count Circular Alternating Binary Windows

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

Problem statement

You are given a nonempty binary array bits, treated as circular, and an integer windowLength.

For every start index in the original array, inspect the next windowLength values around the circle. Count how many such windows strictly alternate between 0 and 1.

Function

countCircularAlternatingWindows(bits: int[], windowLength: int) → int

Examples

Example 1

bits = [1,0,1,1,0]windowLength = 3return = 3

Starts 0, 3, and 4 produce alternating length-three windows.

Example 2

bits = [0,1,0,1]windowLength = 4return = 4

Every rotation of the circular array alternates for four values.

Constraints

  • 1 <= bits.length <= 100000
  • bits[i] is 0 or 1.
  • 1 <= windowLength <= bits.length

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int countCircularAlternatingWindows(int[] bits, int windowLength) {
    // Write your code here.
}
bits[1,0,1,1,0]
windowLength3
expected3
Checking account…