The complement of an integer is the integer you get when you flip all the 0
's to 1
's and all the 1
's to 0
's in its binary representation.
5
is "101"
in binary and its complement is "010"
which is the integer 2
.Given an integer n
, return its complement.
Example 1:
Input: n = 5 Output: 2 Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
Example 2:
Input: n = 7 Output: 0 Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
Example 3:
Input: n = 10 Output: 5 Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
Constraints:
0 <= n < 109
Note: This question is the same as 476: https://leetcode.com/problems/number-complement/
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 way to find the complement involves trying every possible bit pattern until we find the one that, when combined with the original number, results in a power of two minus one. We achieve this by systematically testing potential complements.
Here's how the algorithm would work step-by-step:
def complement_of_base_10_integer(number):
power_of_two = 1
# Find the smallest power of two greater than the input number
while power_of_two <= number:
power_of_two *= 2
potential_complement = 0
while True:
# Check if the potential complement is the correct one
if number + potential_complement == power_of_two - 1:
return potential_complement
potential_complement += 1
The goal is to flip all the bits of a number. A clever way to do this is to create a number with all bits set to 1 that's just large enough to cover all the bits in the original number, then subtract the original number from it.
Here's how the algorithm would work step-by-step:
def bitwiseComplement(number_to_complement):
power_of_two = 1
# Find the smallest power of 2 greater than the input number
while power_of_two <= number_to_complement:
power_of_two <<= 1
# Subtract 1 to get a number with all bits set to 1
all_bits_set = power_of_two - 1
# XORing with all ones complements the bits
complement = all_bits_set ^ number_to_complement
return complement
Case | How to Handle |
---|---|
Input is 0 | If n is 0, its binary representation is '0', the complement is '1', which is 1 in decimal, so return 1. |
Input is 1 | The binary rep is 1, complement is 0, return 0. |
Integer Overflow | Check for integer overflow during the bit flipping and complement calculation by either using a larger data type like long, or by applying a modulo operation if the constraints define a maximum possible value. |
Large Input (close to Integer.MAX_VALUE) | Ensure the intermediate binary representation or complement calculation doesn't exceed integer limits; consider using long or BigInteger if necessary and check memory usage. |
Negative Input | The problem specifies a base-10 integer n, which is typically non-negative; clarify with the interviewer, and if negative numbers are permitted, handle 2's complement representation appropriately. |
All bits are already 1 | Handle the case when the binary representation is all 1s, ensuring correct complement is calculated (e.g., 7 becomes 0). |
Leading zeros in binary representation | The algorithm must correctly identify the significant bits of the binary representation without being affected by leading zeros during the conversion to the complement. |
Single bit set (e.g., power of 2) | Ensure the complement calculation functions properly when only one bit is set in the original number (e.g., 2 becomes 0, 4 becomes 3). |