Facebook Pixel

Arithmetic Progression

Given an array of numbers, determine if the array can make an arithmetic progression after rearranging its elements.

An arithmetic progression is a sequence where the difference between consecutive terms is constant. For example, [1, 3, 5, 7] is an arithmetic progression with a common difference of 2.

Input

  • arr: a list of integers

Output

A boolean: true if the array can form an arithmetic progression after rearranging, false otherwise

Examples

Example 1:

Input: arr = [3, 5, 1]

Output: true

Explanation:

  • After sorting: [1, 3, 5]
  • Differences: 3-1=2, 5-3=2
  • Common difference is 2, so this forms an arithmetic progression

Example 2:

Input: arr = [1, 2, 4]

Output: false

Explanation:

  • After sorting: [1, 2, 4]
  • Differences: 2-1=1, 4-2=2
  • Different differences (1 and 2), so this does NOT form an arithmetic progression

Example 3:

Input: arr = [1, 3]

Output: true

Explanation:

  • Any array with 2 or fewer elements can always form an arithmetic progression

Try It Yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro