The Lost Coordinate
MediumAcc. 92.4%
+35 XP 15
Gauss Summation Logic
Given a sequence of numbers from 0 to N, where one number is missing, you can find the ghost value using simple math rather than nested loops.
The Assignment
Your function receives an array nums.
- Calculate the expected sum of a complete sequence from
0tonums.length.- Formula: $Sum = N imes (N + 1) / 2$ where $N$ is
nums.length.
- Formula: $Sum = N imes (N + 1) / 2$ where $N$ is
- Calculate the actual sum of the elements currently in the array using a loop.
- Subtract the actual sum from the expected sum.
- Print the resulting missing number.
01EXAMPLE 1
Input
nums = [3, 0, 1, 4]Output
2Explanation: Expected sum for N=4 is 10. Actual is 8. Missing is 2.
Constraints
- Use the (N * (N+1)) / 2 formula for efficiency.
- O(n) time complexity required.
ArraysMathAlgorithms
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
nums[3,0,1,4]
Expected Output
2
Click RUN to test your solution