Skip to content
EasyArraysAI interview only

Can Make Arithmetic Progression From Sequence

Asked atamazongooglemicrosoftadobe

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

Example 01
Input
arr = [3,5,1]
Output
true

Sorted, the values are 1, 3, 5, and each step is +2.

Example 02
Input
arr = [1,2,4]
Output
false

Sorted, the steps are +1 and +2; no ordering gives a single common difference.

Example 03
Input
arr = [7,7,7]
Output
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)
05 · Two ways to work on it

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.