Can Make Arithmetic Progression From Sequence
01 · Problem
You are given an integer array arr. An arithmetic progression is a sequence in which the difference between every pair of neighbouring elements is the same constant.
Decide whether the elements of arr can be reordered so that they form an arithmetic progression. Return true if some ordering works, otherwise false. Every element must be used exactly once, and duplicates are allowed (a constant difference of 0 is valid).
02 · Examples
arr = [3,5,1]
true
Sorted, the values are 1, 3, 5, and each step is +2.
arr = [1,2,4]
false
Sorted, the steps are +1 and +2; no ordering gives a single common difference.
arr = [7,7,7]
true
All steps are 0, which is a valid common difference.
03 · Constraints
- 012 <= arr.length <= 1000
- 02-106 <= arr[i] <= 106
- 03arr may contain duplicate values
04 · Optimal complexity
- Time
- O(n log n)
- Space
- O(n)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.