Count Set Bits Problem Statement
Given a positive integer N
, compute the total number of '1's in the binary representation of all numbers from 1 to N. Return this count modulo 1e9+7 because the result can be very 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
, the value for each test case.
Output:
For each test case, return an integer denoting the count of set bits from 1 to N
modulo 1e9+7.
Output each result on a new line.
Example:
Input:
2
3
5
Output:
4
7
Explanation:
For N = 3
, the numbers are 1 (0b1), 2 (0b10), and 3 (0b11). Total set bits = 4.
For N = 5
, the numbers are 1 (0b1), 2 (0b10), 3 (0b11), 4 (0b100), 5 (0b101). Total set bits = 7.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10^9
- Time limit: 1 second
Note:
Implement the function and return the results, as the output mechanism has been handled.

AnswerBot
4mo
Count the total number of set bits in the binary representation of numbers from 1 to N modulo 1e9+7.
Iterate through numbers from 1 to N and count the set bits in their binary representation
Use bitwise...read more
Help your peers!
Add answer anonymously...
Hewlett Packard Enterprise Full Stack Developer interview questions & answers
A Full Stack Developer was asked Q. Merge Sort Problem Statement You are given a sequence of numbers, ARR. Your task...read more
A Full Stack Developer was asked Q. Count Set Bits Problem Statement Given a positive integer N, compute the total n...read more
A Full Stack Developer was asked Q. Smallest Number with Given Digit Product Given a positive integer 'N', find and ...read more
Popular interview questions of Full Stack Developer
A Full Stack Developer was asked Q1. Merge Sort Problem Statement You are given a sequence of numbers, ARR. Your task...read more
A Full Stack Developer was asked Q2. Count Set Bits Problem Statement Given a positive integer N, compute the total n...read more
A Full Stack Developer was asked Q3. Smallest Number with Given Digit Product Given a positive integer 'N', find and ...read more
>
Hewlett Packard Enterprise Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

