The complement of a positive integer is obtained by flipping every bit in its binary representation, considering only the bits up to and including its highest set bit (no leading zeros are flipped).
One line: a positive integer n.
One line: the complement of n.
Example 1
Input
5
Expected
2
Explanation
5 is 101; flipping within 3 bits gives 010 = 2.
Example 2
Input
1
Expected
0
Explanation
1 is a single bit; flipping gives 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →