Given an integer n
, return an array ans
of length n + 1
such that for each i
(0 <= i <= n
), ans[i]
is the number of 1
's in the binary representation of i
.
Example 1:
Input: n = 2 Output: [0,1,1] Explanation: 0 --> 0 1 --> 1 2 --> 10
Example 2:
Input: n = 5 Output: [0,1,1,2,1,2] Explanation: 0 --> 0 1 --> 1 2 --> 10 3 --> 11 4 --> 100 5 --> 101
Constraints:
0 <= n <= 105
Follow up:
O(n log n)
. Can you do it in linear time O(n)
and possibly in a single pass?__builtin_popcount
in C++)?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach directly computes the number of '1' bits for every number in the given range. We will examine each number individually, one after another. We count the number of set bits in each number and save that count.
Here's how the algorithm would work step-by-step:
def counting_bits_brute_force(number_range): results = []
# Iterate through each number in the range.
for number in range(number_range + 1):
binary_representation = bin(number)[2:]
bit_count = 0
# Count the number of '1' bits. Necessary for the problem statement.
for bit in binary_representation:
if bit == '1':
bit_count += 1
results.append(bit_count)
return results
The key is to notice a pattern in how the number of set bits changes as numbers increase. We can reuse previously calculated results to efficiently determine the number of set bits for each new number.
Here's how the algorithm would work step-by-step:
def counting_bits(number):
result_array = [0] * (number + 1)
# Base case: 0 has zero set bits.
result_array[0] = 0
for index in range(1, number + 1):
# Even numbers have the same number of set bits as half themselves.
if index % 2 == 0:
result_array[index] = result_array[index // 2]
# Odd numbers have one more set bit than the even number before them.
else:
result_array[index] = result_array[index // 2] + 1
return result_array
Case | How to Handle |
---|---|
Input n is zero | Return an array containing only zero, representing the count of bits for 0 |
Input n is a large number near the maximum integer value | Check for integer overflow to prevent incorrect results with dynamic programming or bit manipulation techniques |
Input n is negative | The problem statement does not specify how to handle negative numbers, so explicitly return an error or define the behaviour as counting bits of the two's complement representation |
n equals 1 | Return an array containing [0, 1], which are the bit counts for 0 and 1 |
Values near power of 2, such as 2^k -1 or 2^k + 1 | Ensure the algorithm correctly computes the bit counts in these potentially boundary conditions using bit manipulation |
Maximum sized input (n = MAX_INT) | Consider space and time complexity; dynamic programming could use a large array, and bit manipulation should still be efficient |
Integer Overflow during intermediate calculations | Carefully choose data types and operations to avoid exceeding the maximum representable integer value during any intermediate calculation. |
n equals 2 | Return an array containing [0, 1, 1], which are the bit counts for 0, 1 and 2. |