Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-226

Bit Array Set Range

Problem

Maintain a bit array with range-set and range-clear operations.

num_bits is the array length. operations contains ["set", l, r], ["clear", l, r], or ["count"], which records the number of set bits.

Return the recorded counts.

Hint

Set overwrites clears, so process operations strictly in order.

Test cases

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

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