Search topics...
All Problems

Number Complement

Solution Approach
Was this helpful?
EasybitsExpected: O(log n) time, O(1) space
bit-manipulation

Problem

Given a positive integer, return its complement by flipping all bits in its binary representation.

Example 1:

Input: num = 5
Output: 2
Explanation: 5 in binary is 101, its complement is 010 = 2.

Example 2:

Input: num = 1
Output: 0
Explanation: 1 in binary is 1, its complement is 0.
Reference solution unlocks after your first submission
Loading...