Problem overview
The method countBits(n: Int): Array[Int] returns an array of n + 1 numbers. Entry i is the number of 1 bits in the binary form of i, for every i from 0 to n.
For n = 5 the numbers are 0, 1, 10, 11, 100, 101 in binary, and the result is [0, 1, 1, 2, 1, 2]. For n = 0 the only number is 0, so the result is [0].
Approach: reuse a smaller answer
Counting the bits of each number separately costs up to 17 steps per number for these sizes, and the extra work isn't needed. Each answer can be built from one that is already known.
Shifting a number right by one place drops its last bit: 13 is 1101, and 13 shifted right is 110, which is 6. So 13 has the 1 bits of 6, plus its own last bit. That last bit is i & 1: 1 when i is odd, 0 when it is even.
So bits(i) = bits(i >> 1) + (i & 1). Since i >> 1 is smaller than i, its count is already in the array if we fill from 0 upward. This is a small dynamic-programming table, with one constant-time step per number.
Step by step
- Create
val bits = Array.fill(n + 1)(0). It can be aval: the loop changes elements, not the array itself.bits(0)is already right, since 0 has no 1 bits. - Loop
for (i <- 1 to n). Counting upward matters, so the smaller entry is filled when it is needed.1 to nincludesn, and it is empty whennis 0. - Set
bits(i) = bits(i >> 1) + (i & 1). - Return
bits.
The solution in Scala
class Solution {
def countBits(n: Int): Array[Int] = {
val bits = Array.fill(n + 1)(0)
for (i <- 1 to n) {
bits(i) = bits(i >> 1) + (i & 1)
}
return bits
}
}
Scala details
>>is the right shift and&is bitwise AND, both onInt. Arrays are indexed with parentheses:bits(i).- The parentheses around
i & 1matter. In Scala+binds tighter than&, sobits(i >> 1) + i & 1means(bits(i >> 1) + i) & 1. Fori = 13, wherebits(6)is 2, the correct expression gives 3, while the version without parentheses gives 1. 1 to nis a range that includesn;1 until nwould leave it out and the last entry would stay 0.- A
valarray is mutable inside:valstops you from pointingbitsat another array, not from writingbits(i) = ....
Testing
Results from the lesson's method:
2 -> [0, 1, 1]
5 -> [0, 1, 1, 2, 1, 2]
0 -> [0]
13 -> [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3]
For n = 100000 the array has 100001 entries.
Complexity
Time is O(n): each number from 1 to n takes a shift, an AND and an addition. Extra space is O(1) beyond the returned array, because only the loop variable is kept.
Edge cases and common mistakes
- n = 0: the loop doesn't run and the result is
[0], a one-element array. - Array size: the array needs
n + 1entries, because both 0 andnare included. With sizen, the last write throwsArrayIndexOutOfBoundsException. - Starting the loop at 0 or counting downward: starting at 0 is harmless, since
0 >> 1is 0, but counting downward would read entries that aren't filled yet. - Operator precedence: as shown above, keep the parentheses around
i & 1. - Even versus odd: even numbers have the same count as half of them, because the shift only drops a 0; odd numbers have one more.
