Can Make Arithmetic Progression From Sequence - Problem
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: The array [3, 5, 1] can be rearranged to [1, 3, 5] which forms an arithmetic progression with common difference 2.
Input & Output
Example 1 — Basic Case
$
Input:
arr = [3,5,1]
›
Output:
true
💡 Note:
We can rearrange the array as [1,3,5] which forms an arithmetic progression with common difference 2.
Example 2 — Already Sorted
$
Input:
arr = [1,2,4]
›
Output:
false
💡 Note:
The differences are 2-1=1 and 4-2=2. Since 1 ≠ 2, it cannot form an arithmetic progression.
Example 3 — Equal Elements
$
Input:
arr = [1,1,1]
›
Output:
true
💡 Note:
All elements are the same, so the common difference is 0. This forms an arithmetic progression.
Constraints
- 2 ≤ arr.length ≤ 1000
- -106 ≤ arr[i] ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [3,5,1] - check if can form AP
2
Process
Sort to [1,3,5] and check differences
3
Output
Return true since differences are equal
Key Takeaway
🎯 Key Insight: Sort first, then verify all consecutive differences are identical - this is the most efficient approach.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code