Missing Number In Arithmetic Progression — LeetCode 1228 Python Solution
- Problem
- #1228
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- arr = [5,7,11,13]
- Output
- 9
- Explanation
- The previous array was [5,7,9,11,13].
Python solution
class Solution:
def missingNumber(self, arr: List[int]) -> int:
return (arr[0] + arr[-1]) * (len(arr) + 1) // 2 - sum(arr)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1228. Missing Number In Arithmetic Progression is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1228. Missing Number In Arithmetic Progression?
- LeetCode 1228. Missing Number In Arithmetic Progression is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1228. Missing Number In Arithmetic Progression?
- The Python solution on this page runs in O(n), where n is the length of the array.
- What is the space complexity of LeetCode 1228. Missing Number In Arithmetic Progression?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1228. Missing Number In Arithmetic Progression cover?
- LeetCode 1228. Missing Number In Arithmetic Progression is tagged Array and Math on LeetCode.
- Is LeetCode 1228. Missing Number In Arithmetic Progression a premium problem?
- Yes. LeetCode 1228. Missing Number In Arithmetic Progression is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.