|
| 1 | +# Missing Number |
| 2 | + |
| 3 | +**Difficulty:** Easy |
| 4 | +**Category:** Arrays, Bit Manipulation |
| 5 | +**Leetcode Link:** [Problem Link](https://leetcode.com/problems/missing-number/) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 📝 Introduction |
| 10 | + |
| 11 | +Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, the task is to find the single number missing from this range. The input guarantees exactly one missing number. |
| 12 | + |
| 13 | +**Constraints:** |
| 14 | +- `n` numbers, each unique, ranging from 0 to n. |
| 15 | +- Exactly one number in this range is missing. |
| 16 | +- Expected output: the missing number. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 💡 Approach & Key Insights |
| 21 | + |
| 22 | +The main idea is to leverage the **XOR property**: |
| 23 | +- XOR of a number with itself is 0 (`x ^ x = 0`). |
| 24 | +- XOR of a number with 0 is the number itself (`x ^ 0 = x`). |
| 25 | +- XOR is commutative and associative. |
| 26 | + |
| 27 | +If we XOR all indices (0 to n) with all numbers in the array, the duplicates cancel out, leaving only the missing number. |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 🛠️ Breakdown of Approaches |
| 32 | + |
| 33 | +### 1️⃣ Brute Force / Naive Approach |
| 34 | + |
| 35 | +- **Explanation:** |
| 36 | + Loop through numbers from `0` to `n` and check if each is present in the array. Return the number that is not found. |
| 37 | +- **Time Complexity:** O(n²) — because for each number we scan the array. |
| 38 | +- **Space Complexity:** O(1) — no extra space needed apart from variables. |
| 39 | + |
| 40 | +Example: |
| 41 | +Input: `[3, 0, 1]` |
| 42 | +Step 1: Check `0` → Found |
| 43 | +Step 2: Check `1` → Found |
| 44 | +Step 3: Check `2` → Missing → Output: `2` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### 2️⃣ Optimized Approach (Sum Formula) |
| 49 | + |
| 50 | +- **Explanation:** |
| 51 | + Use the sum formula for the first `n` integers: |
| 52 | + `expectedSum = n * (n + 1) / 2` |
| 53 | + Subtract the actual sum of the array from `expectedSum` to get the missing number. |
| 54 | +- **Time Complexity:** O(n) — single pass to get the sum. |
| 55 | +- **Space Complexity:** O(1) — only variables for sums. |
| 56 | + |
| 57 | +Example: |
| 58 | +Input: `[3, 0, 1]` |
| 59 | +n = 3 → Expected Sum = `6` |
| 60 | +Actual Sum = `4` |
| 61 | +Missing = `6 - 4 = 2` |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +### 3️⃣ Best / Final Optimized Approach (XOR Method) |
| 66 | + |
| 67 | +- **Explanation:** |
| 68 | + XOR all numbers from `0` to `n` with all elements of the array. All pairs cancel out except the missing number. |
| 69 | + This avoids integer overflow (unlike sum approach for very large n) and still runs in O(n). |
| 70 | +- **Time Complexity:** O(n) — single pass. |
| 71 | +- **Space Complexity:** O(1) — no extra space. |
| 72 | + |
| 73 | +Example: |
| 74 | +Input: `[3, 0, 1]` |
| 75 | +``` |
| 76 | +xor = 0 |
| 77 | +xor ^ 0 ^ 3 = 3 |
| 78 | +xor ^ 1 ^ 0 = 4 (in binary cancels out) |
| 79 | +xor ^ 2 = 2 (final answer) |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## 📊 Complexity Analysis |
| 85 | + |
| 86 | +| Approach | Time Complexity | Space Complexity | |
| 87 | +| ------------- | --------------- | ---------------- | |
| 88 | +| Brute Force | O(n²) | O(1) | |
| 89 | +| Optimized | O(n) | O(1) | |
| 90 | +| Best Approach | O(n) | O(1) | |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +## 📉 Optimization Ideas |
| 95 | + |
| 96 | +The XOR method is already optimal for both time and space. Any further optimization would focus on language-specific performance tricks, but asymptotic complexity cannot be improved. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## 📌 Example Walkthroughs & Dry Runs |
| 101 | + |
| 102 | +Example: |
| 103 | +Input: `nums = [3, 0, 1]` |
| 104 | +n = 3 |
| 105 | +``` |
| 106 | +xor = 0 |
| 107 | +i = 0 → xor = 0 ^ 0 ^ 3 = 3 |
| 108 | +i = 1 → xor = 3 ^ 1 ^ 0 = 2 |
| 109 | +i = 2 → xor = 2 ^ 2 ^ 1 = 1 |
| 110 | +Final: xor = 1 ^ 3 = 2 |
| 111 | +Output: 2 |
| 112 | +``` |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## 🔗 Additional Resources |
| 117 | + |
| 118 | +- [LeetCode Discussion on XOR Trick](https://leetcode.com/problems/missing-number/discuss) |
| 119 | +- [Bit Manipulation Basics](https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/) |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +Author: Kailash Senthilkumar |
| 124 | +Date: 09/08/2025 |
0 commit comments