Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
Note: If array is sorted, then the binary search will be the optimal for searching.
Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
Hash Set Approach
Time complexity: O(n)
Space complexity: O(n)
defmissingNumber(self,nums: List[int])->int: num_set =set(nums)# set() return array with distinct elementsfor num in nums:if num notin num_set:return num
Bi Manipulation
Because we know that nums contains nn numbers and that it is missing exactly one number on the range [0..n-1][0..n−1], we know that nn definitely replaces the missing number in nums. Therefore, if we initialize an integer to nn and XOR it with every index and value, we will be left with the missing number. Consider the following example (the values have been sorted for intuitive convenience, but need not be):
If no missing number, the sum should be n(n+1)/2. Since array only contain distinct numbers, the missing number will equal to the difference between actual sum and sum of natural numbers up to n.