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 num, return its complement.
Example 1:
Input: num = 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Example 2:
Input: num = 1 Output: 0 Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
Constraints:
1 <= num < 231Note: This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/
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 method for finding the number complement involves checking all possible numbers to find the correct one. We will systematically try each potential complement until we identify the number that, when added to the original number, results in a power of two minus one.
Here's how the algorithm would work step-by-step:
def number_complement_brute_force(number):
power_of_two = 1
# Find the smallest power of 2 greater than the number
while power_of_two <= number:
power_of_two *= 2
max_value = power_of_two - 1
# Iterate through possible complements
for possible_complement in range(max_value + 1):
# Check if current number is the complement
if number + possible_complement == max_value:
return possible_complementTo find the complement of a number, we want to flip all of its bits (0s become 1s and 1s become 0s). The trick is to create a mask of all 1s that's the same length as the original number's binary representation and then use that mask to flip the bits.
Here's how the algorithm would work step-by-step:
def find_complement(number):
# Find the number of bits
number_of_bits = 0
temp_number = number
while temp_number > 0:
temp_number >>= 1
number_of_bits += 1
# Construct a mask with all bits set to 1
# The mask should have the same number of bits as the input number
mask = (1 << number_of_bits) - 1
# XOR operation flips the bits and gives us the complement
complement = number ^ mask
return complement| Case | How to Handle |
|---|---|
| Input number is 1 | The binary representation is '1', flipping it results in '0', so return 0. |
| Input number is the maximum integer value (2^31 - 1 or 2^63-1 depending on the language) | Handle potential overflow when calculating the mask by using a wider integer type if available or by iterative shifting. |
| Input is 0 (while the problem specifies positive, robustness is good) | Return 0 immediately if input is 0; some definitions might see the complement of 0 as 0. |
| Large number with many leading zeros after flipping | The mask calculation correctly identifies the number of significant bits, eliminating the need for post-processing to trim leading zeros. |
| Number is a power of 2 minus 1 (e.g., 3, 7, 15) | The complement should be zero, and the bit manipulation handles this correctly because all bits are 1 initially. |
| Negative input (invalid according to problem description, but good to check) | Throw an exception or return an error code to indicate invalid input, as negative numbers are not valid inputs according to the problem statement. |
| Input is a very large power of 2 | Ensure the bit mask calculation handles very large powers of 2 without overflowing, using appropriate bitwise operations and data types to accommodate the number of bits. |
| Language-specific integer size limits cause unexpected behavior | Use unsigned integers if available to avoid issues with sign bits and represent the number in its pure binary form without sign complications. |