Imagine you have a sequence of numbers that follow a perfect arithmetic pattern - like stepping stones placed at equal distances. But one stone has gone missing!
You're given an array arr that originally contained numbers in arithmetic progression, meaning the difference between consecutive numbers was constant. For example, in the sequence [2, 4, 6, 8, 10], the common difference is 2.
However, one number has been removed from somewhere in the middle (never the first or last element). Your task is to find this missing number.
Goal: Return the missing number that would complete the arithmetic progression.
Input: An array with one missing element from an arithmetic sequence
Output: The missing number as an integer
Example: If the original sequence was [1, 3, 5, 7, 9] and 5 was removed, you'd get [1, 3, 7, 9]. The answer would be 5.
Input & Output
Visualization
Time & Space Complexity
Single pass through the array to find the position where the pattern breaks
Only using constant extra space for calculations
Constraints
-
3 โค
arr.lengthโค 1000 - The missing element is never the first or last element
- arr represents an arithmetic progression with one missing element
-
All elements in
arrare unique integers - -106 โค arr[i] โค 106