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.length1 <= n <= 1040 <= nums[i] <= nnums 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:
Imagine you are given a complete sequence of numbers from a starting value up to a target ending value, but one of the numbers is missing. The brute force approach involves going through the full set of expected numbers one by one and checking if each individual number is present in the collection you were given.
Here's how the algorithm would work step-by-step:
def find_missing_number_brute_force(given_numbers):
expected_minimum = 0
expected_maximum = len(given_numbers)
# Iterate through the complete sequence of expected numbers from minimum to maximum
for current_number in range(expected_minimum, expected_maximum + 1):
is_number_found = False
# Search the entire collection to determine if the current expected number is present
for element in given_numbers:
if element == current_number:
is_number_found = True
break
# Return immediately if the expected number is absent from the given collection
if not is_number_found:
return current_number
return -1Instead of checking every single item one by one to find the missing piece, we can use a clever mathematical shortcut. By knowing the total expected sum of a complete sequence, we can simply find the sum of what we currently have and see what is missing to make up the difference.
Here's how the algorithm would work step-by-step:
def find_missing_number(sequence_numbers):
highest_sequence_value = len(sequence_numbers)
# We calculate the expected sum using the standard arithmetic progression formula to find the complete total.
expected_total_sum = highest_sequence_value * (highest_sequence_value + 1) // 2
current_actual_sum = sum(sequence_numbers)
# The missing value is precisely the gap between what the sum should be and what it actually is.
return expected_total_sum - current_actual_sum| Case | How to Handle |
|---|---|
| Empty or null input array | Return zero or throw an appropriate argument exception based on the function signature contract. |
| Minimum sized input array with a single element | Compare the single element against the expected range to immediately deduce the missing number. |
| Maximum sized input array approaching system memory limits | Use the mathematical sum formula approach to operate in O(1) space complexity and avoid allocation overhead. |
| Integer overflow during sum calculation | Cast the array elements and range bounds to 64-bit integers before computing the arithmetic progression sum. |
| The missing number is at the very beginning of the range | The expected sum will exceed the actual sum by the exact value of the starting boundary, which is zero. |
| The missing number is at the very end of the range | The mathematical sum formula naturally accounts for the upper bound and correctly isolates the missing upper value. |
| Input sequence starts from a non-zero number | Apply a generalized arithmetic sum formula that incorporates the minimum value offset of the sequence range. |