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
class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
for i in range(1, len(arr) - 1):
if (arr[i] << 1) != arr[i - 1] + arr[i + 1]:
return False
return Trueclass Solution {
public boolean canMakeArithmeticProgression(int[] arr) {
Arrays.sort(arr);
for (int i = 1; i < arr.length - 1; ++i) {
if ((arr[i] << 1) != arr[i - 1] + arr[i + 1]) {
return false;
}
}
return true;
}
}/**
* @param {number[]} arr
* @return {boolean}
*/
var canMakeArithmeticProgression = function (arr) {
arr.sort((a, b) => a - b);
for (let i = 1; i < arr.length - 1; i++) {
if (arr[i] << 1 != arr[i - 1] + arr[i + 1]) return false;
}
return true;
};function canMakeArithmeticProgression(arr: number[]): boolean {
arr.sort((a, b) => a - b);
const n = arr.length;
for (let i = 2; i < n; i++) {
if (arr[i - 2] - arr[i - 1] !== arr[i - 1] - arr[i]) {
return false;
}
}
return true;
}function canMakeArithmeticProgression(arr: number[]): boolean {
const n = arr.length;
const map = new Map<number, number>();
let min = Infinity;
let max = -Infinity;
for (const num of arr) {
map.set(num, (map.get(num) ?? 0) + 1);
min = Math.min(min, num);
max = Math.max(max, num);
}
if (max === min) {
return true;
}
if ((max - min) % (arr.length - 1)) {
return false;
}
const diff = (max - min) / (arr.length - 1);
for (let i = min; i <= max; i += diff) {
if (map.get(i) !== 1) {
return false;
}
}
return true;
}impl Solution {
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
arr.sort();
let n = arr.len();
for i in 2..n {
if arr[i - 2] - arr[i - 1] != arr[i - 1] - arr[i] {
return false;
}
}
true
}
}use std::collections::HashMap;
impl Solution {
pub fn can_make_arithmetic_progression(arr: Vec<i32>) -> bool {
let n = arr.len() as i32;
let mut min = i32::MAX;
let mut max = i32::MIN;
let mut map = HashMap::new();
for &num in arr.iter() {
*map.entry(num).or_insert(0) += 1;
min = min.min(num);
max = max.max(num);
}
if min == max {
return true;
}
if (max - min) % (n - 1) != 0 {
return false;
}
let diff = (max - min) / (n - 1);
let mut k = min;
while k <= max {
if *map.get(&k).unwrap_or(&0) != 1 {
return false;
}
k += diff;
}
true
}
}int cmp(const void *a, const void *b) {
return *(int *) a - *(int *) b;
}
bool canMakeArithmeticProgression(int *arr, int arrSize) {
qsort(arr, arrSize, sizeof(int), cmp);
for (int i = 2; i < arrSize; i++) {
if (arr[i - 2] - arr[i - 1] != arr[i - 1] - arr[i]) {
return 0;
}
}
return 1;
}