Skip to content

Commit 275eb2b

Browse files
feat: add Missing Number solution
- Mathematical sum algorithm implementation - Multiple approaches: XOR, Sorting, Hash Set, Binary Search - O(n) time, O(1) space complexity - Comprehensive solutions and optimizations
1 parent 623b49f commit 275eb2b

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/**
2+
* Time Complexity: O(n) - Single pass
3+
* Space Complexity: O(1) - No extra space
4+
*/
5+
class Solution {
6+
public int missingNumber(int[] nums) {
7+
int n = nums.length;
8+
int expectedSum = n * (n + 1) / 2;
9+
int actualSum = 0;
10+
11+
for (int num : nums) {
12+
actualSum += num;
13+
}
14+
15+
return expectedSum - actualSum;
16+
}
17+
}
18+
19+
// Alternative approach using XOR operation
20+
class SolutionXOR {
21+
public int missingNumber(int[] nums) {
22+
int n = nums.length;
23+
int xor = 0;
24+
25+
// XOR all numbers from 0 to n
26+
for (int i = 0; i <= n; i++) {
27+
xor ^= i;
28+
}
29+
30+
// XOR all numbers in array
31+
for (int num : nums) {
32+
xor ^= num;
33+
}
34+
35+
return xor;
36+
}
37+
}
38+
39+
// Alternative approach using sorting
40+
class SolutionSorting {
41+
public int missingNumber(int[] nums) {
42+
Arrays.sort(nums);
43+
44+
for (int i = 0; i < nums.length; i++) {
45+
if (nums[i] != i) {
46+
return i;
47+
}
48+
}
49+
50+
return nums.length;
51+
}
52+
}
53+
54+
// Alternative approach using hash set
55+
class SolutionHashSet {
56+
public int missingNumber(int[] nums) {
57+
Set<Integer> set = new HashSet<>();
58+
59+
for (int num : nums) {
60+
set.add(num);
61+
}
62+
63+
for (int i = 0; i <= nums.length; i++) {
64+
if (!set.contains(i)) {
65+
return i;
66+
}
67+
}
68+
69+
return -1; // Should never reach here
70+
}
71+
}
72+
73+
// Alternative approach using iterative
74+
class SolutionIterative {
75+
public int missingNumber(int[] nums) {
76+
int n = nums.length;
77+
int expectedSum = n * (n + 1) / 2;
78+
int actualSum = 0;
79+
80+
for (int i = 0; i < nums.length; i++) {
81+
actualSum += nums[i];
82+
}
83+
84+
return expectedSum - actualSum;
85+
}
86+
}
87+
88+
// Alternative approach using while loop
89+
class SolutionWhileLoop {
90+
public int missingNumber(int[] nums) {
91+
int n = nums.length;
92+
int expectedSum = n * (n + 1) / 2;
93+
int actualSum = 0;
94+
int i = 0;
95+
96+
while (i < nums.length) {
97+
actualSum += nums[i];
98+
i++;
99+
}
100+
101+
return expectedSum - actualSum;
102+
}
103+
}
104+
105+
// Alternative approach using enhanced for loop
106+
class SolutionEnhancedForLoop {
107+
public int missingNumber(int[] nums) {
108+
int n = nums.length;
109+
int expectedSum = n * (n + 1) / 2;
110+
int actualSum = 0;
111+
112+
for (int num : nums) {
113+
actualSum += num;
114+
}
115+
116+
return expectedSum - actualSum;
117+
}
118+
}
119+
120+
// Alternative approach using recursive
121+
class SolutionRecursive {
122+
public int missingNumber(int[] nums) {
123+
int n = nums.length;
124+
int expectedSum = n * (n + 1) / 2;
125+
int actualSum = sumHelper(nums, 0);
126+
127+
return expectedSum - actualSum;
128+
}
129+
130+
private int sumHelper(int[] nums, int index) {
131+
if (index >= nums.length) {
132+
return 0;
133+
}
134+
135+
return nums[index] + sumHelper(nums, index + 1);
136+
}
137+
}
138+
139+
// Alternative approach using binary search
140+
class SolutionBinarySearch {
141+
public int missingNumber(int[] nums) {
142+
Arrays.sort(nums);
143+
144+
int left = 0, right = nums.length;
145+
146+
while (left < right) {
147+
int mid = left + (right - left) / 2;
148+
149+
if (nums[mid] == mid) {
150+
left = mid + 1;
151+
} else {
152+
right = mid;
153+
}
154+
}
155+
156+
return left;
157+
}
158+
}
159+
160+
// More concise version
161+
class SolutionConcise {
162+
public int missingNumber(int[] nums) {
163+
int n = nums.length;
164+
int expectedSum = n * (n + 1) / 2;
165+
int actualSum = 0;
166+
167+
for (int num : nums) actualSum += num;
168+
169+
return expectedSum - actualSum;
170+
}
171+
}

0 commit comments

Comments
 (0)