-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCountTotalSetBits.cpp
More file actions
54 lines (46 loc) · 1.23 KB
/
Copy pathCountTotalSetBits.cpp
File metadata and controls
54 lines (46 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* GFG
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive).
Example 1:
Input: N = 4
Output: 5
Explanation:
For numbers from 1 to 4.
For 1: 0 0 1 = 1 set bits
For 2: 0 1 0 = 1 set bits
For 3: 0 1 1 = 2 set bits
For 4: 1 0 0 = 1 set bits
Therefore, the total set bits is 5.
Example 2:
Input: N = 17
Output: 35
Explanation: From numbers 1 to 17(both inclusive),
the total number of set bits is 35.
Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits.
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^8
*/
class Solution{
public:
// n: input to count the number of set bits
//Function to return sum of count of set bits in the integers from 1 to n.
int countSetBits(int n)
{
// Your logic here
int ans=0;
for(int i=1;i<=31;i++)
{
int p_div=pow(2,i);
int p_mul=p_div/2;
int temp=(n+1)/p_div;
ans+=(temp*p_mul);
int r=(n+1)%p_div;
if(r>p_mul)
ans+=(r-p_mul);
if(temp==0)
break;
}
return ans;
}
};