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
4mo

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!
Select
Add answer anonymously...

Top Business Analyst Interview Questions Asked at Snapdeal

Q. How would you improve the health of a marketplace?
Q. How would you design a new customer support system?
Q. What are your short-term and long-term goals?
Business Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits