Can Make Arithmetic Progression From Sequence

EasyArraySorting

Description

A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same. Given an array arr, return true if the array can be rearranged to form an arithmetic progression.

Examples

Input:arr = [3,5,1]
Output:true
Explanation:

After sorting [3,5,1] becomes [1,3,5]. Check differences: 3-1=2, 5-3=2. Constant difference means it's an arithmetic progression.

Input:arr = [3, 5, 1]
Output:true
Explanation:

Sorted: [1,3,5]. Common difference is 2, forming an arithmetic progression.

Input:arr = [10, 6, 14, 2]
Output:true
Explanation:

When sorted as [2, 6, 10, 14], the common difference is 4 between each consecutive pair (6-2=4, 10-6=4, 14-10=4).

Constraints

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

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!