Why isnt the following logic working:
(starting from msb)
-
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)] -
if ones < k and (range length - ones)<k then we have already got the answer(mask)
-
if ones>=k then update l and mask [mask= mask | (1<<i) ]
-
else update r
Please have a look at my solution.
It gives correct solutions for sample test case