Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-221

Bitset Popcount Range

Problem

Count the set bits inside ranges of a bit list.

bits is a list of 0s and 1s and queries is a list of [l, r] pairs. Each answer is the number of 1-bits at indices l through r inclusive.

Return the counts in query order.

Hint

The number of set bits in a range is the sum of the slice.

Test cases

case 1
in: [[1,0,1,1,0],[[0,4],[1,3],[2,2]]]
expected: [3,2,1]
case 2
in: [[],[]]
expected: []
case 3
in: [[0,0,0],[[0,2]]]
expected: [0]
case 4
in: [[1,1],[[0,1]]]
expected: [2]
Starter codePython
def bitset_popcount_range(bits, queries):
    # Your code here
    pass

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