'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Topics

NINJA'S COMPLEMENT

Easy
0/40
Average time to solve is 20m
profile
Contributed by
3 upvotes
Asked in companies
LinkedInInfosys

Problem statement

Everyday ninja sees a number in his dream but he doesn’t realize the meaning of that number. Somehow ninja manages to get the secret of his number. He comes to know that if he converts that number into binary and takes its complement and again converts it into decimal type he will get a lottery winning number of ‘Chit fund’ lottery.

But ninja is not good at maths so he is very sad about how he will get that lottery number from his dream’s number. So help our ninja in getting that lottery number.

Your task is to convert the given number into its binary form then take its complement ( Complement of a binary number is the value obtained by swapping all the bits i.e if ‘1’ is present you swap it with ‘0’ and vice versa ), then again convert it into its decimal representation.

Detailed explanation ( Input/output format, Notes, Images )

Sample Input 1:

2
4
6

Sample Output 1:

3
1

Explanation for sample input 1:

Test Case 1:
For the first test case, the given number is ‘4’ so its binary representation is ‘1 0 0’ after taking its complement it looks like ‘0 1 1’.  So now converting it into decimal we get ‘3’ so it’s our final answer.

Test Case 2:
For the first test case, the given number is ‘6’ so its binary representation is ‘1 1 0’ after taking its complement it looks like ‘0 0 1’.  So now converting it into decimal we get ‘1’ so it’s our final answer.

Sample Input 2:

1
9

Sample Output 2:

6

Explanation of sample input 2:

Test Case 1:
For the first test case, the given number is ‘9’ so its binary representation is ‘1 0 0 1’ after taking its complement it looks like ‘0 1 1 0’.  So now converting it into decimal we get ‘6’ so it’s our final answer.
Full screen
Console