Problem: Count Total Set Bits
Given a positive integer A, the task is to count the total number of set bits in the binary representation of all the numbers from 1 to A Return the count modulo 109 + 7.
1 <= A <= 109
The first and only argument is an integer A.
Return an integer denoting the ( Total number of set bits in the binary representation of all the numbers from 1 to A )modulo 109 + 7.
Input 1: A = 3 Input 2: A = 1
Output 1: 4 Output 2: 1
DECIMAL BINARY SET BIT COUNT 1 01 1 2 10 1 3 11 2 1 + 1 + 2 = 4 Answer = 4 % 1000000007 = 4
A = 1 DECIMAL BINARY SET BIT COUNT 1 01 1 Answer = 1 % 1000000007 = 1
Solution in C++:
If you have any questions or queries, feel free to drop a comment in the comments section below.
Note: Help us improve this blog
If you have a better solution, and you think you can help your peers to understand this problem better, then please drop your solution and approach in the comments section below.
We will upload your approach and solution here by giving you the proper credit so that you can showcase it among your peers.