Missing Number (XOR)

EasyArrayMathBit Manipulation

Description

Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Use O(1) extra space and O(n) runtime.

Examples

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

Array has n=3 elements, so range is [0,1,2,3]. Array contains {0,1,3}. Using XOR or sum formula, This finds 2 is missing.

Input:nums = [1]
Output:0
Explanation:

The missing number is 0.

Input:nums = [0,1,2,3,5,6,7]
Output:4
Explanation:

The missing number is 4.

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 10⁴
  • 0 ≤ nums[i] ≤ n

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!