FastPrepBinary Sorting Rounds After Flips

Binary Sorting Rounds After Flips

Visa logoVisa● HardINTERNOA
Learn

Problem statement

You are given a binary string binary and an array flips. Process the flip indices in order. Each flip changes the character at that index from 0 to 1.

After every flip, define the current sorting-round count as the maximum, over all remaining 0 characters, of the number of 1 characters before that zero. If no zero remains, the sorting-round count is 0.

Return the sorting-round count after each flip.

Function

getSortingRounds(binary: String, flips: int[]) → int[]

Examples

Example 1

binary = "10100"flips = [1,3]return = [3,4]

After flipping index 1, the remaining zeros are at indices 3 and 4, each with three ones before it. After flipping index 3, the final zero has four ones before it.

Example 2

binary = "000"flips = [1,0,2]return = [1,2,0]

The first flip leaves the last zero with one preceding one. The second leaves it with two preceding ones. The final flip removes the last zero, so the answer becomes 0.

Constraints

  • 1 <= binary.length <= 2 * 10^5.
  • binary contains only 0 and 1.
  • 1 <= flips.length <= binary.length.
  • Every value in flips is a distinct 0-based index whose current character is 0.

More Visa problems

See Visa hiring insights
public int[] getSortingRounds(String binary, int[] flips) {
  // write your code here
}
binary"10100"
flips[1,3]
expected[3,4]
Checking account…