LeetCode : Number Complement

Prem Parmar
Competitive Programming Problems
1 min readOct 11, 2020
Photo by Alexander Sinn on Unsplash

You are given a positive integer number, output is its complement number.

Complement number means inverse of the binary representation of that number.

Binary representation is in the form of 1’s and 0’s. We will inverse 0 with 1 and vice versa.

Let us take example :

Suppose one number 11 (In decimal) and its binary representation is “1011”. The complement number of 11 is “0100” (In binary) equals to 4 (In decimal).

We are using XOR, It can revert bit if we do XOR with bit 1.

Here, We are using Wrapper Class Integer’s function toBinaryString() which is used to get Binary representation of number and we will find length and using that we do XOR. this is the solution of of that problem and you can get this solution from below mentioned gist link:

https://gist.github.com/premparmar11/b6265a3ad98f6efe4ea3f0f00dc86f50

--

--

Prem Parmar
Competitive Programming Problems

Software Engineer, having 3 years of experience in Ecommerce / HCM domain Product based company.