Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Example 1:
Input: 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: 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.

Note:
  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.
  3. This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/

class Solution {
    public int findComplement(int num) {
       
        int totalBits = (int)(Math.floor(Math.log(num) / Math.log(2))) + 1;
        return (int)(Math.pow(2, totalBits)-1) ^ num;
       
    }
}

Here, first we are going to find total number of bits in number.
Then power(2, total number of bits)-1 and XOR of given number produces the complement of given number.

[Example]

Take num = 5
 
Total bits is 3, using log(5)/log(3)+1, neglect decimal value .
Then, pow(2, 3)-1 [which describes maximum decimal number formed using n number of total bits, here total bits is 3, which means maximum number formed will be 7(111)].
Finally, XOR of maximum number and given number will produce result. [7(111) ^ 5(101) = 2(010)]


Runtime beats 100% of submissions

Related Posts:

Click here for May month challenges with solution and explanation
Click here for April month challenges with solution and explanation


Please let us know your views in comment section

Subscribe us to receive updates instantly..

Happy Coding..!!

Comments

Popular posts from this blog

First Unique Character in a String

Balanced Binary Tree

Majority Element

Sequential Digits