Missing Number In Arithmetic Progression - Problem
In some array arr, the values were in arithmetic progression: the differences 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 the modified array arr, return the removed value.
Input & Output
Example 1 — Basic Case
$
Input:
arr = [1,3,7,9]
›
Output:
5
💡 Note:
The original arithmetic progression was [1,3,5,7,9] with common difference 2. The number 5 was removed, so we return 5.
Example 2 — Negative Numbers
$
Input:
arr = [15,13,9]
›
Output:
11
💡 Note:
The original sequence was [15,13,11,9] with common difference -2. The missing number is 11.
Example 3 — Larger Difference
$
Input:
arr = [5,7,11,13]
›
Output:
9
💡 Note:
The original sequence was [5,7,9,11,13] with common difference 2. The number 9 was removed.
Constraints
- 3 ≤ arr.length ≤ 1000
- -105 ≤ arr[i] ≤ 105
- The given array represents a valid arithmetic progression with one missing element
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array with one missing element from AP
2
Process
Calculate expected vs actual sum or find gap
3
Output
Return the missing number
Key Takeaway
🎯 Key Insight: Use the arithmetic progression sum formula to find the missing element efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code