Can Make Arithmetic Progression From Sequence

A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.

Given an array of numbers arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.

Example 1
Inputarr = [3,5,1]
Outputtrue
We can reorder the elements as [1, 3, 5] or [5, 3, 1] with differences 2 and -2 respectively between each consecutive element.
Example 2
Inputarr = [1,2,4]
Outputfalse
There is no way to reorder the elements to obtain an arithmetic progression.

Constraints

  • 2 <= arr.length <= 1000
  • -10^6 <= arr[i] <= 10^6

Asked at 5 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate