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
Can Make Arithmetic Progression: Input → Process → OutputInput Array:351SORT & CHECKAfter Sorting:135Check Differences:✓ 3 - 1 = 2✓ 5 - 3 = 2All differences equal!OUTPUTtrueArithmetic Progression: 1, 3, 5 (common difference = 2)
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.
Asked in
Google 15 Facebook 12 Amazon 8
28.0K Views
Medium Frequency
~10 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen