In some array arr, the values were in arithmetic progression: the values arr[i + 1] - arr[i] are all equal for every 0 <= i < arr.length - 1.
A value from arr was removed that was not the first or last value in the array.
Given arr, return the removed value.
Example 1:
Input: arr = [5,7,11,13] Output: 9 Explanation: The previous array was [5,7,9,11,13].
Example 2:
Input: arr = [15,13,12] Output: 14 Explanation: The previous array was [15,14,13,12].
Constraints:
3 <= arr.length <= 10000 <= arr[i] <= 105When 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:
The problem asks us to find a single missing number in an arithmetic progression. Because the first and last elements are guaranteed not to be missing, we can deduce the constant difference directly from them. We then iterate through the array until the difference between consecutive elements does not match the expected step.
Here's how the algorithm would work step-by-step:
def missing_number(arr):
# Calculate the total number of elements in the complete progression.
total_elements = len(arr) + 1
# Determine the common difference using the first and last elements.
common_difference = (arr[-1] - arr[0]) // (total_elements - 1)
# Traverse the array to locate the missing value.
for index in range(len(arr) - 1):
expected_value = arr[index] + common_difference
if arr[index + 1] != expected_value:
return expected_value
return arr[0]
The arithmetic progression is missing exactly one element that is neither the first nor the last term. By determining the total difference between the ends and dividing by the expected number of steps, we can deduce the true common difference. Using binary search, we locate the earliest index where the actual value deviates from the expected value to find the missing number.
Here's how the algorithm would work step-by-step:
def find_missing_number(arr: list[int]) -> int:
# Calculate the expected common difference of the full progression
total_diff = (arr[-1] - arr[0]) // len(arr)
left_index = 0
right_index = len(arr) - 1
# Use binary search to locate where the discrepancy begins
while left_index < right_index:
mid_index = (left_index + right_index) // 2
expected_value = arr[0] + mid_index * total_diff
# Check if the arithmetic sequence holds up to mid_index
if arr[mid_index] == expected_value:
left_index = mid_index + 1
else:
right_index = mid_index
# The missing number is at the identified gap position
return arr[0] + left_index * total_diff
| Case | How to Handle |
|---|---|
| Input array with fewer than three elements | Since at least three elements are required to determine the progression and the missing spot, we must validate input size and throw an error or return default. |
| Array with a constant difference of zero | The arithmetic difference can be zero if all elements are identical, which we handle by calculating the difference safely without division by zero. |
| Missing number is at the very beginning of the sequence | Comparing differences across multiple index intervals ensures we correctly identify the step size even when the first term is absent. |
| Missing number is at the very end of the sequence | Checking the difference at both the start and end of the sorted array allows us to detect missing terminal values accurately. |
| Input containing large negative and positive numbers | Using standard arithmetic subtraction handles negative and positive ranges seamlessly without sign distortion. |
| Unsorted input array | Sorting the array first in logarithmic time ensures that consecutive elements are adjacent for straightforward difference checking. |
| Extremely large inputs approaching memory or time limits | A binary search approach running in logarithmic time or an optimal linear scan avoids timeouts and memory overflow. |
| Integer overflow during difference calculations | Casting integers to 64-bit equivalents prevents overflow when computing differences between extremely large boundary values. |