Missing Number In Arithmetic Progression - Problem

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

example_1.py โ€” Basic Case
$ Input: [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.py โ€” Negative Numbers
$ Input: [15, 13, 9, 7]
โ€บ Output: 11
๐Ÿ’ก Note: The original sequence was [15, 13, 11, 9, 7] with common difference -2. The missing number 11 was removed.
example_3.py โ€” Larger Differences
$ Input: [5, 15, 25, 45]
โ€บ Output: 35
๐Ÿ’ก Note: Original sequence: [5, 15, 25, 35, 45] with common difference 10. The number 35 is missing.

Visualization

Tap to expand
Finding the Missing Stepping Stone135Missing!792 units4 units (2ร—2)2 units๐Ÿ’ก Key InsightExpected step = (last - first) รท count = (9 - 1) รท 4 = 2Missing stone creates a gap exactly twice the normal step size!
Understanding the Visualization
1
Measure the Total Distance
Calculate the span from first to last stone: 9 - 1 = 8 units
2
Find Expected Step Size
With 4 stones visible, there should be 5 total with 4 gaps: 8 รท 4 = 2 units per step
3
Walk and Check Each Gap
Step 1โ†’3: 2 units โœ“, Step 3โ†’7: 4 units โœ— (too big!), Step 7โ†’9: 2 units โœ“
4
Find the Missing Stone
The gap of 4 should be two steps of 2, so missing stone is at 3 + 2 = 5
Key Takeaway
๐ŸŽฏ Key Insight: In any arithmetic progression with one missing element, that missing element creates exactly one gap that's twice the normal step size. Use the mathematical formula to calculate the expected step, then find where this pattern breaks!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array to find the position where the pattern breaks

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for calculations

n
2n
โœ“ Linear Space

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 arr are unique integers
  • -106 โ‰ค arr[i] โ‰ค 106
Asked in
Google 45 Amazon 38 Microsoft 32 Apple 28
67.0K Views
Medium Frequency
~15 min Avg. Time
1.9K 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