File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 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+ */
You can’t perform that action at this time.
0 commit comments