Counting Bits
Count the 1s in the binary form of every number from 0 to n, reusing the count of a smaller number.
Given a whole number nbitsn + 1inbits[i]i
Example 1
Output: [0, 1, 1]
Explanation: in binary, 0 is 0, 1 is 1 and 2 is 10, so they hold zero, one and one 1s.
Example 2
Output: [0, 1, 1, 2, 1, 2]
Explanation: 3 is 11 and 5 is 101, so both hold two 1s.
The Idea
A computer stores every number as a row of bits, each 0 or 1. Each bit stands for a power of two. The rightmost bit, at position 0, is worth 1, the bit at position 1 is worth 2, the next is worth 4, and so on. So 5 is written 101, which is 4 + 1, and it holds two 1s.
Take Example 2, with n = 5. In binary, the numbers 0 to 5 are 0, 1, 10, 11, 100 and 101, holding 0, 1, 1, 2, 1 and 2 ones. That is the output [0, 1, 1, 2, 1, 2]
Look at the picture below. Each row shows a number with its binary digits at positions 2, 1 and 0, and the box with the orange outline in each row is the last bit, at position 0.
Cross off the last bit, and the bits that are left form a smaller number, shown in the column "without the last bit". In the orange row, 5 is 101. Without its last bit it is 10, which is 2, and the bit that was crossed off is 1.
The teal row for 2 sits higher up. Since the rows are filled from the top down, its count is already there: 2 holds one 1. So 5 holds that one, plus its last bit, 1 + 1 = 2. The arrow labelled "already known" shows that step. The other rows work the same way. Take 4, which is 100: without the last bit it is 2 again, and its last bit is 0, so 4 holds 1 + 0 = 1.
So, written from 0 upward, each new count is one earlier count plus one bit. Filling the counts in that order, where each one reuses a count already written, is called dynamic programming. Here the table being filled is the answer itself: bits[i]i
Solution Approach
Turning that idea into code means seeing why counting each number on its own is slower, then settling how Java splits off the last bit and which order fills the array safely. Removing the lowest 1 bit, instead of the last bit, is an optional alternative.
Counting each number on its own
Why not count the 1s of every number separately? A loop can check the bits of iInteger.bitCount(i)i
Splitting off the last bit
How does Java give us the number without its last bit, and that bit itself? A Java int
The shift i >> 15 >> 1i & 1ii & 1i5 & 1
bits[i] = bits[i >> 1] + (i & 1)
The order to fill the array
Is bits[i >> 1]bits[i]ii >> 1iibits[0] = 0int
Removing the lowest 1 bit (optional)
Does the smaller number have to be the one without the last bit? Another choice is iii & (i - 1)6 & 5
That smaller number holds exactly one 1 fewer, so bits[i] = bits[i & (i - 1)] + 1bits[6]bits[4]Integer.bitCount
Java Solution
The method countBits
- First, create the answer array with
, one entry for each number from 0 toint[] bits = new int[n + 1] . Java fills it with 0s, son is already right.bits[0] - Then a loop moves
from 1 up toi .n - Inside the loop, set
tobits[i] , the count forbits[i >> 1] without its last bit, plusi , the last bit itself.(i & 1) - Once the loop has filled every entry, the method returns
.bits
The mainInteger.bitCount
import java.util.Arrays;
public class CountingBits {
public static int[] countBits(int n) {
int[] bits = new int[n + 1]; // bits[0] stays 0
for (int i = 1; i <= n; i++) {
bits[i] = bits[i >> 1] + (i & 1); // count without the last bit, plus that bit
}
return bits;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(countBits(2)));
System.out.println(Arrays.toString(countBits(5)));
System.out.println(Arrays.toString(countBits(0)));
System.out.println(Arrays.toString(countBits(16)));
int[] big = countBits(100000);
boolean same = true;
for (int i = 0; i <= 100000; i++) {
same &= big[i] == Integer.bitCount(i);
}
System.out.println("same as Integer.bitCount for 0 to 100000: " + same);
}
}
Running it prints:
[0, 1, 1]
[0, 1, 1, 2, 1, 2]
[0]
[0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1]
same as Integer.bitCount for 0 to 100000: true
With n = 5 the method returns [0, 1, 1, 2, 1, 2][0]
Look at the end of the fourth line. The number 15, which is 1111, holds four 1s, but 16, which is 10000, holds just one. Its count comes from 8, which comes from 4, then 2, then 1, each holding a single 1. The last line shows that all 100,001 counts agree with Integer.bitCount
The parentheses in (i & 1)+&bits[i >> 1] + i & 1(bits[i >> 1] + i) & 1[0, 1, 1, 0, 1, 0]
Complexity
The method does the same small amount of work for each number from 1 to n.
- Time: O(n), because the loop fills each of the n entries after
with one shift, one AND, one array read and one addition.bits[0] - Space: O(1) besides the output, because the
array of n + 1 entries is the answer itself, and the loop keeps onlybits .i