-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathCountingBits.cpp
216 lines (150 loc) · 4.92 KB
/
CountingBits.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Source: https://leetcode.com/problems/counting-bits/
5 Approaches
1st Approach (brute force)
Time: O(n log base2 n)
Space: O(n), vector of size n is required
*/
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res;
res.push_back(0);
for(int i = 1; i <= n; ++i) {
int num = i;
int setBits = 0;
while(num != 0) {
if((num & 1) != 0) { // is equivalent to num % 2 != 0 to check whether the number is even or odd
++setBits;
}
num >>= 1;
}
res.push_back(setBits);
}
return res;
}
};
----------------------------------------------------------------------------------------------------------
/*
2nd Approach (using Brian Kernighan Algorithm)
Refer to the following link for detailed explanation of Brian Kernighan Algorithm
https://leetcode.com/problems/number-of-1-bits/discuss/1519675/JAVA-C%2B%2B-%3A-Simple-or-O(1)-Time-or-Faster-than-100-or-Detailed-Explanation
Time: O(n log base2 n)
Space: O(n), vector of size n is required
*/
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res;
res.push_back(0);
for(int i = 1; i <= n; ++i) {
int num = i;
int setBits = 0;
while(num != 0) {
num &= num - 1;
++setBits;
}
res.push_back(setBits);
}
return res;
}
};
-----------------------------------------------------------------------------------------------------------
/*
3rd Approach (more optimized than 2nd approach based on observation of the pattern formed)
A number can be either even or odd, and
Last bit of even number is 0 and odd number is 1
decimal binary
1 --> 1 (1 set bit)
2 --> 10 (1 set bit)
3 --> 11 (2 set bits)
4 --> 100 (1 set bit)
5 --> 101 (2 set bits)
6 --> 110 (2 set bits)
7 --> 111 (3 set bits)
8 --> 1000 (1 set bit)
9 --> 1001 (2 set bits)
10 --> 1010 (2 set bits)
when number is even
suppose x = 6, now x / 2 = 3, here number of set bits in x(i.e. 6) are 2 and set bits in x / 2(i.e. 3) are also 2
so, we can say that when number is even, number of set bits in x = number of set bits in (x / 2)
and when number is odd
suppose x = 9, now x - 1 = 8, here number of set bits in x(i.e. 9) are 2 and set bits in x - 1(i.e. 8) are 1
so, we can say that when number is odd, number of set bits in x = number of set bits in (x - 1) + 1
Based on this observation, we can solve this problem in O(n)
Time: O(n)
Space: O(n), vector of size n is required
*/
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res(n + 1);
res[0] = 0;
for(int i = 1; i <= n; ++i) {
if((i & 1) == 0) { // equivalent to (i % 2 == 0) to check whether the number is even or odd
res[i] = res[i >> 1]; // (i >> 1) is equivalent to (i / 2)
} else {
res[i] = res[i - 1] + 1;
}
}
return res;
}
};
-----------------------------------------------------------------------------------------------------------
/*
4th Approach (more optimized than 3rd approach also based on observation)
A number can be either even or odd, and
Last bit of even number is 0 and odd number is 1
decimal binary
1 --> 1 (1 set bit)
2 --> 10 (1 set bit)
3 --> 11 (2 set bits)
4 --> 100 (1 set bit)
5 --> 101 (2 set bits)
6 --> 110 (2 set bits)
7 --> 111 (3 set bits)
8 --> 1000 (1 set bit)
9 --> 1001 (2 set bits)
10 --> 1010 (2 set bits)
when number is even
suppose x = 6, now x / 2 = 3, here number of set bits in x(i.e. 6) are 2 and set bits in x / 2(i.e. 3) are also 2
so, we can say that when number is even, number of set bits in x = number of set bits in (x / 2)
and when number is odd
suppose x = 9, now x / 2 = 4, here number of set bits in x(i.e. 9) are 2, but set bits in x / 2(i.e. 4) are 1
so, we can say that when number is odd, number of set bits in x = number of set bits in (x / 2) + 1
Based on this observation, we can solve this problem in O(n)
Time: O(n)
Space: O(n), vector of size n is required
*/
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res(n + 1);
res[0] = 0;
for(int i = 1; i <= n; ++i) {
if((i & 1) == 0) { // equivalent to (i % 2 == 0) to check whether the number is even or odd
res[i] = res[i >> 1]; // (i >> 1) is equivalent to (i / 2)
} else {
res[i] = res[i >> 1] + 1;
}
}
return res;
}
};
-----------------------------------------------------------------------------------------------------------
/*
5th Approach (more optimized version of 4th approach without using if/else condition)
Time: O(n)
Space: O(n), vector of size n is required
*/
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res(n + 1);
res[0] = 0;
for(int i = 1; i <= n; ++i) {
res[i] = res[i >> 1] + (i & 1);
}
return res;
}
};