EasyNo. 1228LeetCode Premium

Missing Number In Arithmetic Progression — LeetCode 1228 Python Solution

Topics
Array, Math
Reading time
2 min

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

PythonSolution
class Solution:
    def missingNumber(self, arr: List[int]) -> int:
        return (arr[0] + arr[-1]) * (len(arr) + 1) // 2 - sum(arr)

Complexity

Time
O(n)where n is the length of the array
Space · auxiliary
O(1)

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

Related problems
No.ProblemDifficulty
0268Missing NumberEasy
1232Check If It Is a Straight LineEasy
0048Rotate ImageMedium
0066Plus OneEasy
0189Rotate ArrayMedium
0204Count PrimesMedium

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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.