Count Set Bits Problem Statement

Given a positive integer N, find the total number of '1's in the binary representation of all the numbers from 1 to N.

You should return the result modulo 109+7 as the count can be large.

Input:

The first line contains an integer ‘T’ representing the number of test cases. 
Each of the next ‘T’ lines contains a single integer ‘N’.

Output:

For each test case, output a single integer which is the count of set bits in the binary representation of all numbers from 1 to N, modulo 109+7. 
Each test case's output should be printed on a new line.

Example:

Suppose T = 2 with the following test cases:

Test case 1:
N = 3

Test case 2:
N = 5
Output:
Test case 1 output:
4

Test case 2 output:
7

Explanation:

For test case 1, numbers are 1, 10, and 11 in binary, with a total of 4 set bits.
For test case 2, numbers are 1, 10, 11, 100, and 101, totaling 7 set bits.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 109
  • Time limit of 1 second
AnswerBot
4d

Count the total number of set bits in the binary representation of numbers from 1 to N, modulo 10^9+7.

  • Iterate through numbers from 1 to N and count the set bits in their binary representation

  • Use bitwi...read more

Help your peers!
Add answer anonymously...
Snapdeal Business Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter