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.

  1. Calculate the expected sum of a complete sequence from 0 to nums.length.
    • Formula: $Sum = N imes (N + 1) / 2$ where $N$ is nums.length.
  2. Calculate the actual sum of the elements currently in the array using a loop.
  3. Subtract the actual sum from the expected sum.
  4. Print the resulting missing number.

01EXAMPLE 1

Inputnums = [3, 0, 1, 4]
Output2

Explanation: 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
JavaScript
Loading...
3 Hidden

Input Arguments

nums[3,0,1,4]

Expected Output

2

Click RUN to test your solution