Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-227

Bit Array Flip Range

Problem

Maintain a bit array with range-flip operations.

num_bits is the array length. operations contains ["flip", l, r] (toggle every bit in the inclusive range) or ["count"] (record the number of set bits).

Return the recorded counts.

Hint

Flipping the same range twice restores the original bits.

Test cases

case 1
in: [4,[["flip",1,2],["count"],["flip",0,1],["count"]]]
expected: [2,2]
case 2
in: [0,[["count"]]]
expected: [0]
case 3
in: [3,[["flip",0,2],["count"]]]
expected: [3]
case 4
in: [5,[["flip",0,4],["flip",0,4],["count"]]]
expected: [0]
Starter codePython
def bit_array_flip_range(num_bits, operations):
    # Your code here
    pass

4 test cases · run your code against them in the editor