Missing Number

Given an array containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Pattern focus: XOR Trick. XOR all indices and values to cancel matched pairs and isolate the missing value.

Input Format

nums = array of distinct integers from 0 to n with one number missing

Output Format

the missing number

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • nums must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

nums = [3,0,1]

Output:

2

Explanation:

The numbers 0, 1, and 3 are present, so 2 is missing.

Example 2:

Input:

nums = [0,1]

Output:

2

Explanation:

For n = 2, the missing number is 2.

Loading...
Missing Number - Bit Manipulation DSA Problem