Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Counting Bits
Bookmark
Input
Example 1
Example 2
Example 3
Custom
n
=
5
0
1
2
3
4
5
0
0
0
0
0
0
0
1
2
3
4
5
0
0
0
0
0
0
0
1
2
3
4
5
0
0
0
0
0
0
0
1
2
3
4
5
0
1
0
0
0
0
i
i&(i-1)
i
0
0
1
= 1
i&(i-1)
0
0
0
= 0
ans[1]
=
ans[0]
+ 1
=
1
0
1
2
3
4
5
0
1
0
0
0
0
i
i&(i-1)
i
0
1
0
= 2
i&(i-1)
0
0
0
= 0
ans[2]
=
ans[0]
+ 1
0
1
2
3
4
5
0
1
1
0
0
0
i
i&(i-1)
i
0
1
0
= 2
i&(i-1)
0
0
0
= 0
ans[2]
=
ans[0]
+ 1
=
1
0
1
2
3
4
5
0
1
1
2
0
0
i
i&(i-1)
i
0
1
1
= 3
i&(i-1)
0
1
0
= 2
ans[3]
=
ans[2]
+ 1
=
2
0
1
2
3
4
5
0
1
1
2
0
0
i
i&(i-1)
i
1
0
0
= 4
i&(i-1)
0
0
0
= 0
ans[4]
=
ans[0]
+ 1
0
1
2
3
4
5
0
1
1
2
1
0
i
i&(i-1)
i
1
0
0
= 4
i&(i-1)
0
0
0
= 0
ans[4]
=
ans[0]
+ 1
=
1
0
1
2
3
4
5
0
1
1
2
1
2
i
i&(i-1)
i
1
0
1
= 5
i&(i-1)
1
0
0
= 4
ans[5]
=
ans[4]
+ 1
=
2
0
1
2
3
4
5
0
1
1
2
1
2
[0, 1, 1, 2, 1, 2]
algo
master
.
io
Step:
Count the 1-bits of every number from 0 to 5. Reuse earlier answers: ans[i] = ans[i & (i-1)] + 1
0 / 12
Input
Example 1
Example 2
Example 3
Custom
n
=
5
0 / 12
algo
master
.
io
Step:
Count the 1-bits of every number from 0 to 5. Reuse earlier answers: ans[i] = ans[i & (i-1)] + 1