Skip to content
Decoder

Complement of Base 10 Integer

Easy

Problem

The complement of a number flips every bit of its binary form (without leading zeros): 1s become 0s and 0s become 1s. For example 5 is 101, whose complement is 010, which is 2. Given n, return its complement.

Example 1

Input
n = 5
Output
2
Why
101 → 010

Example 2

Input
n = 7
Output
0
Why
111 → 000

Example 3

Input
n = 10
Output
5
Why
1010 → 0101

Example 4

Input
n = 0
Output
1
Why
0 → 1

Constraints

  • 0 <= n < 10^9

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.