Skip to content

Commit 3170494

Browse files
committed
solve: missing number
1 parent 618c7df commit 3170494

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

missing-number/evan.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var missingNumber = function (nums) {
6+
const expectedSum = (nums.length * (nums.length + 1)) / 2;
7+
const actualSum = nums.reduce((prev, curr) => prev + curr);
8+
9+
return expectedSum - actualSum;
10+
};
11+
12+
/**
13+
* Time Complexity: O(n)
14+
* Reason:
15+
* - Finding the maximum number in the array takes O(n) time.
16+
* - Calculating the target sum takes O(1) time.
17+
* - Iterating through the array to update the target number takes O(n) time.
18+
* - Checking the condition and returning the result takes O(1) time.
19+
* - Therefore, the overall time complexity is O(n).
20+
*
21+
* Space Complexity: O(1)
22+
* Reason:
23+
* - The algorithm uses a constant amount of extra space (variables like maxNumber, hasZero, targetNumber).
24+
* - No additional space proportional to the input size is used.
25+
* - Therefore, the overall space complexity is O(1).
26+
*/

0 commit comments

Comments
 (0)