DAND - Editorial

Why isnt the following logic working:

(starting from msb)

  1. for index i(numbered from left) and current ans=mask, find count of numbers(ones) that are in the range and of the form (mask|(1<<i))
    eg, for range=[8,11],mask=1000 and i=1 we can get ones = 2 [10(1010) and 11(1011)]

  2. if ones < k and (range length - ones)<k then we have already got the answer(mask)

  3. if ones>=k then update l and mask [mask= mask | (1<<i) ]

  4. else update r

Please have a look at my solution.

It gives correct solutions for sample test case

1 Like