-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingBits.java
More file actions
200 lines (151 loc) · 4.24 KB
/
CountingBits.java
File metadata and controls
200 lines (151 loc) · 4.24 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
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
package Algorithms.BitManipulation;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 17 May 2025
*/
public class CountingBits {
public static void main(String[] args) {
int n = 5;
System.out.println("countBits => " + countBits(n));
}
/**
* 0 - 0 dp[0] = 0
* 1 - 1 dp[n-1] + 1
* 2 - 10 dp[n-2] + 1
* 3 - 11 dp[n-2] + 1
* 4 - 100 dp[n-4] + 1
* 5 - 101 dp[n-4] + 1
* 6 - 110 dp[n-4] + 1
* 7 - 111 dp[n-4] + 1
* 8 - 1000 dp[n-8] + 1
* 9 - 1001 dp[n-8] + 1
* 10 - 1010 dp[n-8] + 1
* 11 - 1011 dp[n-8] + 1
* 12 - 1100 dp[n-8] + 1
* 13 - 1101 dp[n-8] + 1
* 14 - 1110 dp[n-8] + 1
* 15 - 1111 dp[n-8] + 1
* 16 - 10000 dp[n-16] + 1
* 17 - 10001
* 18 - 10010
* 19 - 10011
* 20 - 10100
* 21 - 10101
* 22 - 10110
* 23 - 10111
* 24 - 11000
* 25 - 11001
* 26 - 11010
* 27 - 11011
* 28 - 11100
* 29 - 11101
* 30 - 11110
* 31 - 11111
* 32 - 100000
* 33 - 100001
*
* We see that 00, 01, 10, 11, 100, 101, 110, 111 pattern is repeating --> 8
*/
public static int[] countBits(int n) {
int[] dp = new int[n + 1];
int offset = 1;
for (int i=1; i<=n; i++) {
if (offset * 2 == i) {
offset = i;
}
dp[i] = 1 + dp[i - offset];
}
return dp;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*/
public static int[] countBits2(int n) {
int[] res = new int[n + 1];
for (int i = 1; i <= n; i++) {
res[i] = res[i & (i - 1)] + 1;
}
return res;
}
public int[] countBits3(int n) {
int[] res = new int[n + 1];
for (int i = 1; i <= n; i++) {
res[i] = res[i >> 1] + (i & 1); // or res[i/2] + i%2 ---> dividing by 2 is same as right shift by 1 bit i.e i>>1
}
return res;
}
public int[] countBits4(int n) {
int[] res = new int[n+1];
load(res,1,1);
return res;
}
private void load(int[] res,int ind,int val){
if(ind >= res.length)
return;
res[ind] = val;
load(res,ind*2,val);
load(res,ind*2+1,val+1);
}
public int[] countBits5(int n) {
int[] ret = new int[n + 1];
int off = 1;
for (int i = 0; i < ret.length - 1; i++){
if (off * 2 == i + 1) off *= 2;
ret[i + 1] = ret[i + 1 - off] + 1;
}
return ret;
}
public int[] countBits6(int n) {
int[] res=new int[n+1];
for(int i=0;i<=n;i++) res[i]=gSB(i);
return res;
}
private int gSB(int n){
int count=0;
for(int i=0;i<=31;i++){
if((n&(1<<i))>0) count++;
}
return count;
}
/**
* @TimeComplexity O(nlogn)
* @SpaceComplexity O(n)
*/
public int[] countBitsUsingBinaryString(int n) {
int[] ans = new int[n+1];
for(int i=0; i<=n; i++) {
int num = i;
while(num>0) { // O(log n) TimeComplexity because of num=num/2
if(num%2 == 1) ans[i]++;
num=num/2;
}
/**
String binary = "";
int num = 2;
while (num > 0) {
binary = (num % 2) + binary; // O(log n) TimeComplexity
num /= 2;
}
System.out.println(binary);
// Integer.toBinaryString(10) ---> to convert num to binary
// Integer.parseInt(binaryStr, 2) ---> to convert binary to num
*/
}
return ans;
}
public int[] countBitsUsingBinaryString2(int n) {
int[] result = new int[n+1];
result[0]=0;
for(int i=1;i<=n;i++){
int count = 0;
int temp = i;
while(temp>0){
if((temp&1) == 1) count++;
temp= temp>>1; // or temp>>=1
}
result[i] = count;
}
return result;
}
}