Solution: Complement of Base 10 Integer
Let's solve the Complement of Base 10 Integer problem using the Bitwise Manipulation pattern.
Statement
For any number in base 10, return the complement of its binary representation as an integer in base 10.
Constraints
Solution
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
Naive approach
To calculate the complement of any integer, we need to perform the following steps:
-
Convert the integer to its binary value.
-
Once we have the binary value, we can use a loop to incrementally convert each to and each to .
-
Now that we have the complemented binary number, we can convert it to its respective base 10 value.
The conversion of binary values from to and to can take up to iterations, where is the length of binary value of the base 10 number. So, the time complexity of this approach is . Since the number of bits needed to hold the binary value is , the space complexity of this approach is also .
Optimized approach using bitwise manipulation
This approach uses bitwise XOR operations, which outputs only when the two bits compared are different and when they are the same. It XORs the binary value of the number with a bitmask composed entirely of . The length of the bitmask is equal to the bit length of the original number, ensuring each bit is flipped: become , and become . After taking XOR, it converts the binary value back to base to get the final answer.
The following illustration demonstrates the algorithm:
The primary goal here is to flip all the bits of a number. Here’s how we’ll implement this algorithm:
-
Calculate the number of bits required to store a certain integer. We can get this by rounding down and adding .
-
Create an all 1-bit bitmask against the number of bits of the input value. For example, if we need bits to represent an integer, all its set bits will be
1111
. The decimal value of1111
, which is equal to . So, we can use this formula to get the required number. -
Flip all occurrences of s to s and s to s by computing the XOR operation between the two numbers.
Let’s look at the code for this solution below:
class ComplementNumber {public static int findBitwiseComplement(int num) {if (num == 0) {return 1;}int bitCount = (int) Math.floor((int)(Math.log(num) / Math.log(2))) + 1;int allBitsSet = (1 << bitCount) - 1;return num ^ allBitsSet;}public static void main(String[] args) {int[] decimalValues = {42, 233, 100, 999999, 54};for (int i = 0; i < decimalValues.length; i++) {System.out.print(i + 1);System.out.print(".\tInput: " + decimalValues[i]);System.out.print("\n\tBitwise complement of " + decimalValues[i] + " is: ");System.out.println(findBitwiseComplement(decimalValues[i]));System.out.println(new String(new char[100]).replace('\0', '-'));}}}
Solution summary
To recap, the solution to this problem can be divided into the following parts:
-
We calculate the number of bits required to store any number.
-
We create an all 1-bit bitmask against it and flip all occurrences of s to s and s to s by computing the XOR operation.
-
By converting the binary value back to base 10, we got our final answer.
Time complexity
To take the complement, we first calculate the number of bits required for the binary representation of the given integer and then take XOR, which are constant time operations. So, the time complexity of this solution is .
Space complexity
The space complexity of this solution is because at any time we have a decimal number to have it converted into its complement.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.