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
Missing Number in Arithmetic ProgressionOriginal: [1,3,5,7,9] → Given: [1,3,7,9] → Find: 513579Missing+2+2+2+2Given Array:1379Expected Sum: 25, Actual Sum: 20Missing Number: 25 - 20 = 5
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
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
25.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen