What is the most efficient approach for this?

Simple approach i used :-

#include
using namespace std;

int main()
{
int q;
cin >> q;

while(q–)
{
int a, b;
cin >> a >> b;

   int setBit = 0;
   for(int i = a; i <= b; i++)
   {
       setBit += __builtin_popcount(i);
   }
   cout << setBit << endl;

}
}

hi @devang_11912089
u can go through this https://www.geeksforgeeks.org/count-total-set-bits-in-all-numbers-from-range-l-to-r/ for other optimized methods…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.