Skip to content
BASA DSA

Missing Number

EasyBit Manipulation

An array contains n distinct values from 0..n. Return the single missing value using linear time and constant auxiliary space.

Example 1

Input
nums = [3,0,1]
Output
2

Explanation

Among 0,1,2,3 only 2 is absent.

Example 2

Input
nums = []
Output
0

Explanation

For n=0 the allowed set is {0}, so the missing value is 0.

Example 3

Input
nums = [0,1]
Output
2

Explanation

Length two includes value 2 in the allowed range even though it is not an index.

Constraints

  • 0 <= n <= 10000
  • Values are distinct and within 0..n

Function contractmissingNumberArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…