Skip to content
Onsite

Missing Number

EasyP2Coding

nums contains N - 1 distinct integers taken from the range 1..N. Exactly one number of that range is missing. Return it.

N is not given; it is nums.length + 1. The elements may be in any order.

Example 1

Input
nums = [1,2,4,5]
Output
3
Why
3 is missing from 1..5.

Example 2

Input
nums = [2,3,1,5]
Output
4

Example 3

Input
nums = []
Output
1

Constraints

  • 0 <= nums.length <= 10^5
  • The values are distinct and lie in 1..nums.length + 1

Hints

Run checks the 3 visible tests, Submit all 10.