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] <= 106
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to see if a list of numbers can be rearranged to form a perfectly spaced number sequence, like 2, 4, 6, 8. The brute force approach tries every possible order of the numbers until a perfect sequence is found or all orders have been checked.
Here's how the algorithm would work step-by-step:
def can_make_arithmetic_progression_brute_force(numbers):
import itertools
# Generate all possible permutations of the input numbers.
for permutation in itertools.permutations(numbers):
# Need at least two numbers to determine a common difference.
if len(permutation) < 2:
return True
common_difference = permutation[1] - permutation[0]
# Check if the common difference holds for the entire sequence.
is_arithmetic_progression = True
for index in range(2, len(permutation)):
if permutation[index] - permutation[index - 1] != common_difference:
is_arithmetic_progression = False
break
# If progression is found, no need to check other permutations.
if is_arithmetic_progression:
return True
# If no arithmetic progression was found return false.
return False
The best way to check if a sequence can form an arithmetic progression is to first put the numbers in order. Once they're sorted, we can quickly check if the difference between each consecutive pair of numbers is the same.
Here's how the algorithm would work step-by-step:
def can_make_arithmetic_progression(sequence):
sequence.sort()
# Need at least two elements to form a progression.
if len(sequence) < 2:
return True
first_difference = sequence[1] - sequence[0]
# Check if every difference matches the first difference.
for i in range(2, len(sequence)):
current_difference = sequence[i] - sequence[i - 1]
# Arithmetic progression is invalid if differences don't match
if current_difference != first_difference:
return False
return True
Case | How to Handle |
---|---|
Null or empty input array | Return true if the input array is null or has fewer than two elements since a single element or no elements inherently forms an arithmetic progression. |
Array with only two elements | Return true as any two elements trivially form an arithmetic progression. |
Array with all identical values | Return true, as a sequence of identical values has a common difference of 0, forming an arithmetic progression. |
Integer overflow when calculating the difference | Use long data type to store the difference to avoid overflow. |
Array with negative numbers | The solution should correctly handle negative numbers without any modification, as arithmetic progression works with negative differences as well. |
Array contains duplicate values that would result in a misleading 'true' output if not handled correctly | The algorithm needs to count occurrences of each element to make sure there are no more occurrences than expected based on the common difference. |
Large input array leading to potential performance issues if not sorted efficiently | Use an efficient sorting algorithm like mergesort or quicksort, which have an average time complexity of O(n log n). |
The sequence does not form an arithmetic progression. | The algorithm must return false when, after sorting and computing differences, not all consecutive differences are the same. |