-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_set_bits.cpp
More file actions
52 lines (47 loc) · 1.06 KB
/
count_set_bits.cpp
File metadata and controls
52 lines (47 loc) · 1.06 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
#include<bits/stdc++.h>
using namespace std;
// Q.Given a positive integer N, print count of set bits in it. eg.6 ie110 ans->2
int count_set_bits1( int n)
{
if (n==0)
return 0;
else
return 1+ count_set_bits1(n&(n-1));
}
int count_set_bits2( int n)
{
int count=0;
while(n){
count= count+ n&1;
n=n>>1;
}
}
void sortby_no_of_sortbits(vector<int>& arr){
vector<vector<int>> count(32);
//max no of setbits can be 31. so rows represent no of setbits,
//and vector at row i represent arr elms with i no of setbits
for(int i=0; i<arr.size();i++){
int x=count_set_bits2(arr[i]);
count[x].push_back(arr[i]);
}
int n=0;
for(int i=31; i>=0; i--){ //elms with higher no of setbits come first,
for(int j=0; j<count[i].size(); j++){ //if same no of setbits come in original order in arr
arr[n]=count[i][j];
n++;
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
#endif
int n;
cin>>n;
cout<<count_set_bits1(n);
return 0;
}