Logic please

Q-Contest Page | CodeChef

can anyone tell which method is this.

#include <bits/stdc++.h>
using namespace std;

int countMaxSetBits(int left, int right)
while ((left | (left + 1)) <= right)
left |= left + 1;

return left; 


int main()
int t;
int l,r;
cout << countMaxSetBits(l, r) << endl;
return 0;

Can you explain the logic , I still didn’t got the logic even after reading it

Let’s firstly emphasize on what n=(n|n+1) does:

  • For n =12 \equiv 1100 (In binary) :

  • For n=47\equiv 101111

  • For n=71\equiv 1000111

If you work out a few binary additions on paper then you’ll have the following interesting observation.

  • n=(n|n+1) sets the rightmost unset bit of n.

So what this algorithm essentially does is to iteratively keep on turning the rightmost unset bit of n while ensuring in each iteration that n \in[A, B]. So we can be assured that our result from the last iteration will have the maximum number of set bits(since we’re increasing the number of set bits in each iteration) and it will be minimum with that many set bits (since we’re greedily turning on the rightmost unset bit) .


Thanks Man, Now I understood , It really helped

1 Like