Can Make Arithmetic Progression From Sequence (LeetCode 1502): Solution and Explanation
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
- Find
lo = min(arr),hi = max(arr)andn = len(arr). - If
(hi - lo) % (n - 1) != 0, return false. Otherwised = (hi - lo) / (n - 1). - If
d == 0, all values equal lo, so return true. - For each x: if
(x - lo) % d != 0return false; otherwisek = (x - lo) / d, return false if slot k is taken, else mark it. - Return true.
Step-by-step example
arr = [3,5,1]
- lo = 1, hi = 5, n = 3. The span 4 splits into 2 steps, so d = 2 and the slots are 1, 3, 5.
- 3 goes to slot (3 - 1) / 2 = 1, which is free.
- 5 goes to slot 2 and 1 goes to slot 0, both free.
- 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 True1class Solution {
2 public boolean canMakeArithmeticProgression(int[] arr) {
3 int n = arr.length;
4 int lo = Arrays.stream(arr).min().getAsInt();
5 int hi = Arrays.stream(arr).max().getAsInt();
6 if ((hi - lo) % (n - 1) != 0) {
7 return false;
8 }
9 int d = (hi - lo) / (n - 1);
10 if (d == 0) {
11 return true;
12 }
13 boolean[] seen = new boolean[n];
14 for (int x : arr) {
15 if ((x - lo) % d != 0) {
16 return false;
17 }
18 int k = (x - lo) / d;
19 if (seen[k]) {
20 return false;
21 }
22 seen[k] = true;
23 }
24 return true;
25 }
26}1class Solution {
2public:
3 bool canMakeArithmeticProgression(vector<int>& arr) {
4 int n = arr.size();
5 int lo = *min_element(arr.begin(), arr.end());
6 int hi = *max_element(arr.begin(), arr.end());
7 if ((hi - lo) % (n - 1) != 0) {
8 return false;
9 }
10 int d = (hi - lo) / (n - 1);
11 if (d == 0) {
12 return true;
13 }
14 vector<bool> seen(n, false);
15 for (int x : arr) {
16 if ((x - lo) % d != 0) {
17 return false;
18 }
19 int k = (x - lo) / d;
20 if (seen[k]) {
21 return false;
22 }
23 seen[k] = true;
24 }
25 return true;
26 }
27};Time and space complexity
O(n)
One pass for min and max and one pass to place values.
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.