You are given a positive integer n.
Let even denote the number of even indices in the binary representation of n with value 1.
Let odd denote the number of odd indices in the binary representation of n with value 1.
Note that bits are indexed from right to left in the binary representation of a number.
Return the array [even, odd].
Example 1:
Input: n = 50
Output: [1,2]
Explanation:
The binary representation of 50 is 110010.
It contains 1 on indices 1, 4, and 5.
Example 2:
Input: n = 2
Output: [0,1]
Explanation:
The binary representation of 2 is 10.
It contains 1 only on index 1.
Constraints:
1 <= n <= 1000When 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 problem requires us to examine the individual bits of a given number. A brute force approach involves inspecting each bit one by one to determine if it's even, odd, and whether it is a 0 or a 1.
Here's how the algorithm would work step-by-step:
def count_even_odd_bits(number):
even_bits_count = 0
odd_bits_count = 0
bit_position = 0
# Iterate through bits until the number becomes zero
while number > 0:
# Check if the current bit is set (equal to 1)
if number % 2 == 1:
# Increment even/odd counts based on bit position
if bit_position % 2 == 0:
even_bits_count += 1
else:
odd_bits_count += 1
# Shift the number to the right by one bit
number = number // 2
# Update the current bit position
bit_position += 1
return [even_bits_count, odd_bits_count]We need to find which bits in a number are 'on' (equal to 1) at even and odd places. The clever trick is to look at each bit's position directly without converting it into a list or other data structure.
Here's how the algorithm would work step-by-step:
def number_of_even_and_odd_bits(number):
even_bit_count = 0
odd_bit_count = 0
bit_position = 1
# Iterate through the bits of the number
while number > 0:
# Check if the current bit is set (equal to 1)
if number & 1:
# Increment counters based on bit position
if bit_position % 2 == 0:
#Even position
even_bit_count += 1
else:
#Odd position
odd_bit_count += 1
# Move to the next bit by right-shifting
number >>= 1
# Update position counter
bit_position += 1
return [even_bit_count, odd_bit_count]| Case | How to Handle |
|---|---|
| Negative input number | Convert the input number to its absolute value, as bit positions are the same for positive and negative representations of the same magnitude. |
| Input is zero | Return [0,0] since zero has no set bits. |
| Large input number close to the maximum integer value | Ensure the solution correctly handles all bits without integer overflow issues; use bitwise operations carefully. |
| Input is a power of 2 | The solution should correctly identify one odd bit and zero even bits. |
| Input is a number with alternating 0s and 1s in its binary representation | The solution must accurately count the number of even and odd bits when there is a mix of both. |
| Integer overflow when calculating number of bits | Use bitwise operations and avoid arithmetic operations that could lead to overflow. |
| All bits are 1 | The solution needs to count all even and odd positions correctly. |
| Integer with many trailing zeros | The bitwise and shifting operations are efficient at skipping over trailing zeros, thus handling this case efficiently. |