We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 00f2cd3 commit d4daa7bCopy full SHA for d4daa7b
missing-number/hyer0705.ts
@@ -0,0 +1,22 @@
1
+// Time Complexity: O(n)
2
+// Space Complexity: O(1)
3
+function missingNumber(nums: number[]): number {
4
+ const n = nums.length;
5
+
6
+ const expectedSum = (n * (n + 1)) / 2;
7
+ const currentSum = nums.reduce((acc, curr) => acc + curr, 0);
8
9
+ return expectedSum - currentSum;
10
+}
11
12
+// Time Complexity: O(n^2)
13
14
15
16
17
+ for (let i = 0; i <= n; i++) {
18
+ if (!nums.includes(i)) return i;
19
+ }
20
21
+ return -1;
22
0 commit comments