Find the missing number
Coding problemEasyAsked in: Accenture, Coupa
A list holds every whole number from 1 to n except one, in any order, with no repeats. So the list has n - 1 numbers. Return the number that is missing.
The missing number can be 1, n or anything in between. An empty list means n is 1 and the missing number is 1.
Examples
Example 1
Input: nums = [3,1,4]
Output: 2
Example 2
Input: nums = [1,2,3]
Output: 4
n is 4 here, and 4 is the missing number.
Example 3
Input: nums = []
Output: 1
Constraints
- n is between 1 and 500,000
- Every number in the list is between 1 and n and appears once
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.
to answer and keep your progress. Reading is free for everyone.