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.
Example 1:
Input: nums = [3,0,1]
Output: 2
Explanation:
n = 3
since there are 3 numbers, so all numbers are in the range [0,3]
. 2 is the missing number in the range since it does not appear in nums
.
Example 2:
Input: nums = [0,1]
Output: 2
Explanation:
n = 2
since there are 2 numbers, so all numbers are in the range [0,2]
. 2 is the missing number in the range since it does not appear in nums
.
Example 3:
Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation:
n = 9
since there are 9 numbers, so all numbers are in the range [0,9]
. 8 is the missing number in the range since it does not appear in nums
.
Constraints:
n == nums.length
1 <= n <= 104
0 <= nums[i] <= n
nums
are unique.Follow up: Could you implement a solution using only O(1)
extra space complexity and O(n)
runtime complexity?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find a missing number in a sequence. The brute force approach tries every possible number within the expected range to see if it's the missing one.
Here's how the algorithm would work step-by-step:
def find_missing_number_brute_force(numbers):
expected_range_start = 0
expected_range_end = len(numbers)
for potential_missing_number in range(expected_range_start, expected_range_end + 1):
# Check if the current number is present in the input list
if potential_missing_number not in numbers:
#If a number isn't present, we've found our missing number
return potential_missing_number
# If no number is missing from the range, it means 'n' is missing
return expected_range_end + 1
The problem is to find a number missing from a sequence of consecutive numbers. The efficient approach avoids checking each possible number by cleverly using a mathematical formula.
Here's how the algorithm would work step-by-step:
def find_missing_number(number_list):
list_length = len(number_list)
# Calculate the expected sum using the formula for the sum of an arithmetic series.
expected_total = (list_length + 1) * (list_length + 2) // 2
actual_total = sum(number_list)
# Subtract the actual sum from the expected sum to find the missing number.
missing_number = expected_total - actual_total
return missing_number
Case | How to Handle |
---|---|
nums is null or empty | Return 0, as the missing number in an empty range [0, 0] is 0. |
nums contains only one element (n=1) | Check if nums[0] is 0 or 1 and return the other value respectively. |
nums contains all numbers from 0 to n, so the missing number is n+1 (integer overflow if n is close to MAX_INT) | The solution must avoid integer overflow when calculating the sum or expected sum and return n if all numbers from 0 to n-1 are present in nums. |
nums contains only 0 | Return 1, as the missing number is 1 when n = 1 and nums[0] = 0. |
nums contains all numbers except 0 | Return 0, as 0 is the missing number. |
Large input array affecting time and space complexity | Ensure the chosen algorithm has optimal time complexity (O(n) preferably) and consider space constraints. |
n is close to Integer.MAX_VALUE which may cause overflow in sum calculations | Utilize XOR operation or Gauss' Formula (n*(n+1))/2 cautiously, addressing possible overflow. |
nums contains duplicate numbers (although problem states distinct numbers) | The solution should still function correctly, for example, XOR handles duplicates by canceling out pairs. |