|
| 1 | +--- |
| 2 | +id: complement-of-base-10-integer |
| 3 | +title: Complement of Base 10 Integer |
| 4 | +sidebar_label: 1009-Complement of Base 10 Integer |
| 5 | +tags: |
| 6 | + - Bit Manipulation |
| 7 | + - LeetCode |
| 8 | + - Java |
| 9 | + - Python |
| 10 | + - C++ |
| 11 | +description: "This is a solution to the Complement of Base 10 Integer problem on LeetCode." |
| 12 | +sidebar_position: 15 |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem Description |
| 16 | + |
| 17 | +The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation. |
| 18 | + |
| 19 | +For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2. Given an integer `n`, return its complement. |
| 20 | + |
| 21 | +### Examples |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | + |
| 25 | +``` |
| 26 | +Input: n = 5 |
| 27 | +Output: 2 |
| 28 | +Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10. |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | +``` |
| 34 | +Input: n = 7 |
| 35 | +Output: 0 |
| 36 | +Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10. |
| 37 | +``` |
| 38 | + |
| 39 | +**Example 3:** |
| 40 | + |
| 41 | +``` |
| 42 | +Input: n = 10 |
| 43 | +Output: 5 |
| 44 | +Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10. |
| 45 | +``` |
| 46 | + |
| 47 | +### Constraints |
| 48 | + |
| 49 | +- `0 <= n < 10^9` |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## Solution for Complement of Integer Problem |
| 54 | + |
| 55 | +To solve this problem, we need to flip all bits of the binary representation of the given integer `n`. |
| 56 | + |
| 57 | +### Approach |
| 58 | + |
| 59 | +1. **Brute Force:** |
| 60 | + - Convert the integer to its binary string representation. |
| 61 | + - Flip each bit and convert the binary string back to an integer. |
| 62 | + |
| 63 | +2. **Optimized Approach:** |
| 64 | + - Find the number of bits in the binary representation of `n`. |
| 65 | + - Create a mask with the same number of bits set to 1. |
| 66 | + - XOR `n` with the mask to get the complement. |
| 67 | + |
| 68 | +### Code in Different Languages |
| 69 | + |
| 70 | +<Tabs> |
| 71 | +<TabItem value="C++" label="C++" default> |
| 72 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 73 | + |
| 74 | +```cpp |
| 75 | +class Solution { |
| 76 | +public: |
| 77 | + int bitwiseComplement(int n) { |
| 78 | + if (n == 0) return 1; |
| 79 | + int mask = 1; |
| 80 | + while (mask < n) { |
| 81 | + mask = (mask << 1) | 1; |
| 82 | + } |
| 83 | + return n ^ mask; |
| 84 | + } |
| 85 | +}; |
| 86 | +``` |
| 87 | +
|
| 88 | +</TabItem> |
| 89 | +<TabItem value="Java" label="Java"> |
| 90 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 91 | +
|
| 92 | +```java |
| 93 | +class Solution { |
| 94 | + public int bitwiseComplement(int n) { |
| 95 | + if (n == 0) return 1; |
| 96 | + int mask = 1; |
| 97 | + while (mask < n) { |
| 98 | + mask = (mask << 1) | 1; |
| 99 | + } |
| 100 | + return n ^ mask; |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +</TabItem> |
| 106 | +<TabItem value="Python" label="Python"> |
| 107 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 108 | + |
| 109 | +```python |
| 110 | +class Solution: |
| 111 | + def bitwiseComplement(self, n: int) -> int: |
| 112 | + if n == 0: |
| 113 | + return 1 |
| 114 | + mask = 1 |
| 115 | + while mask < n: |
| 116 | + mask = (mask << 1) | 1 |
| 117 | + return n ^ mask |
| 118 | +``` |
| 119 | + |
| 120 | +</TabItem> |
| 121 | +</Tabs> |
| 122 | + |
| 123 | +#### Complexity Analysis |
| 124 | + |
| 125 | +- **Time Complexity**: $O(log n)$, where `n` is the given integer. This is because we are iterating through the bits of `n`. |
| 126 | +- **Space Complexity**: $O(1)$, as we are using a constant amount of extra space. |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +<h2>Authors:</h2> |
| 131 | + |
| 132 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 133 | +{['ImmidiSivani'].map(username => ( |
| 134 | + <Author key={username} username={username} /> |
| 135 | +))} |
| 136 | +</div> |
| 137 | +``` |
0 commit comments