Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 1:
Input: arr = [3,5,1] Output: true Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.
Example 2:
Input: arr = [1,2,4] Output: false Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
Constraints:
2 <= arr.length <= 1000-106 <= arr[i] <= 106Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[3,5,1]
[1,2,4]
arithmetic-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1502: Can Make Arithmetic Progression From Sequence
class Solution {
public boolean canMakeArithmeticProgression(int[] arr) {
Arrays.sort(arr);
int d = arr[1] - arr[0];
for (int i = 2; i < arr.length; ++i) {
if (arr[i] - arr[i - 1] != d) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #1502: Can Make Arithmetic Progression From Sequence
func canMakeArithmeticProgression(arr []int) bool {
sort.Ints(arr)
d := arr[1] - arr[0]
for i := 2; i < len(arr); i++ {
if arr[i]-arr[i-1] != d {
return false
}
}
return true
}
# Accepted solution for LeetCode #1502: Can Make Arithmetic Progression From Sequence
class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
d = arr[1] - arr[0]
return all(b - a == d for a, b in pairwise(arr))
// Accepted solution for LeetCode #1502: Can Make Arithmetic Progression From Sequence
impl Solution {
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
arr.sort();
let n = arr.len();
let d = arr[1] - arr[0];
for i in 2..n {
if arr[i] - arr[i - 1] != d {
return false;
}
}
true
}
}
// Accepted solution for LeetCode #1502: Can Make Arithmetic Progression From Sequence
function canMakeArithmeticProgression(arr: number[]): boolean {
arr.sort((a, b) => a - b);
const n = arr.length;
const d = arr[1] - arr[0];
for (let i = 2; i < n; i++) {
if (arr[i] - arr[i - 1] !== d) {
return false;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.