Can Make Arithmetic Progression From Sequence (LeetCode 1502): Solution and Explanation

Easy
Array
Sorting

Decide whether the numbers in arr can be reordered so that every pair of neighbours has the same difference.

Intuition

If the numbers can form a progression, the smallest one has to be the first term and the largest one the last term. There are n - 1 equal steps between them.

That fixes the step without sorting: d = (max - min) / (n - 1). If that division leaves a remainder, no progression exists.

Now picture a ruler with n slots at min, min + d, min + 2d and so on. Every value must sit exactly on a slot, and no two values may share one. If both hold, the n values fill the n slots exactly once.

Approach: Slot every value on the ruler

  1. Find lo = min(arr), hi = max(arr) and n = len(arr).
  2. If (hi - lo) % (n - 1) != 0, return false. Otherwise d = (hi - lo) / (n - 1).
  3. If d == 0, all values equal lo, so return true.
  4. For each x: if (x - lo) % d != 0 return false; otherwise k = (x - lo) / d, return false if slot k is taken, else mark it.
  5. Return true.

Step-by-step example

arr = [3,5,1]
  1. lo = 1, hi = 5, n = 3. The span 4 splits into 2 steps, so d = 2 and the slots are 1, 3, 5.
  2. 3 goes to slot (3 - 1) / 2 = 1, which is free.
  3. 5 goes to slot 2 and 1 goes to slot 0, both free.
  4. All three slots are filled once, so return true.

Code: Python, Java, C++

1class Solution:
2    def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
3        lo, hi, n = min(arr), max(arr), len(arr)
4        if (hi - lo) % (n - 1) != 0:
5            return False
6        d = (hi - lo) // (n - 1)
7        if d == 0:
8            return True
9        seen = [False] * n
10        for x in arr:
11            if (x - lo) % d != 0:
12                return False
13            k = (x - lo) // d
14            if seen[k]:
15                return False
16            seen[k] = True
17        return True
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Slot every value on the ruler O(n)
Hash set of values O(n)
Sort and compare gaps O(n log n)
Time

O(n)

One pass for min and max and one pass to place values.

Space

O(n)

The slot array has n booleans.

Other approaches

Sort and compare gaps (time O(n log n), space O(1) to O(n))

Sort arr and check that arr[i] - arr[i-1] is the same for every i. The most common answer and perfectly fine for n up to 1000.

Hash set of values (time O(n), space O(n))

Same slot idea, but store values in a set and check that lo + k*d is present for every k, after confirming there are no duplicates (unless d is 0).

Common mistakes

  • Forgetting the d == 0 case, which would divide by zero when computing slots.
  • Skipping the duplicate check. [0,2,4,4,8] has the right span and every value is on a slot, but 4 appears twice and 6 is missing.
  • Checking only the gaps between consecutive input elements without sorting first.

Frequently asked questions

Does a decreasing order count?

Yes. Any order with a constant difference works, so [5,3,1] is as good as [1,3,5]. The check is the same either way.

Why do min and max have to be the ends?

In a progression with a positive step the terms only grow, so the first is the smallest and the last is the largest. A negative step just reverses that.

Is sorting acceptable in an interview?

Yes, it is the expected first answer. The slot method is a nice follow-up when asked for linear time.