Skip to content

Commit 17686d4

Browse files
authored
Create Day28.cpp
1 parent 1703022 commit 17686d4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Day28.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Author: Aryan Yadav
3+
Counting Bits
4+
Complexity: O(n)
5+
Difficulty: Medium
6+
*/
7+
8+
class Solution {
9+
public:
10+
vector<int> countBits(int num) {
11+
vector<int> v(num+1);
12+
for(int i=0;i<=num;i++){
13+
v[i] = v[i>>1] + (i & 1);
14+
}
15+
return v;
16+
}
17+
};
18+
19+
/* Easy Solution */
20+
class Solution {
21+
public:
22+
vector<int> countBits(int num) {
23+
vector<int> v(num+1);
24+
for(int i=0;i<=num;i++){
25+
v[i] = __builtin_popcount(i);
26+
}
27+
return v;
28+
}
29+
};

0 commit comments

Comments
 (0)