Skip to content

Commit 11cc0de

Browse files
Solution & Explanation #268 - Kailash Senthilkumar - 09/08/2025 - commit #1
1 parent 4ec8fe3 commit 11cc0de

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int missingNumber(vector<int>& nums) {
4+
int n = nums.size();
5+
6+
// Initialize xor to 0. This will hold the result of XOR operations.
7+
int xorResult = 0;
8+
9+
// Iterate through the array
10+
for (int i = 0; i < n; i++) {
11+
// XOR current index and the current number
12+
xorResult = xorResult ^ i ^ nums[i];
13+
}
14+
15+
// XOR with the last number n (because indices go from 0 to n-1, but range is 0 to n)
16+
xorResult = xorResult ^ n;
17+
18+
// The result is the missing number
19+
return xorResult;
20+
}
21+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int missingNumber(int[] nums) {
3+
int n = nums.length;
4+
5+
// Initialize xor to 0. This will hold the result of XOR operations.
6+
int xor = 0;
7+
8+
// Iterate through the array
9+
for (int i = 0; i < n; i++) {
10+
// XOR current index and the current number
11+
xor = xor ^ i ^ nums[i];
12+
}
13+
14+
// XOR with the last number n (because indices go from 0 to n-1, but range is 0 to n)
15+
xor = xor ^ n;
16+
17+
// The result is the missing number
18+
return xor;
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def missingNumber(self, nums):
3+
n = len(nums)
4+
5+
# Initialize xor to 0. This will hold the result of XOR operations.
6+
xor = 0
7+
8+
# Iterate through the array
9+
for i in range(n):
10+
# XOR current index and the current number
11+
xor = xor ^ i ^ nums[i]
12+
13+
# XOR with the last number n (because indices go from 0 to n-1, but range is 0 to n)
14+
xor = xor ^ n
15+
16+
# The result is the missing number
17+
return xor

0 commit comments

Comments
 (0)