Can We Make Arithmetic Progression From Sequence of Numbers?

  • 时间:2020-09-08 11:08:55
  • 分类:网络文摘
  • 阅读:147 次

Given an array of numbers arr. A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same. 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
-10^6 <= arr[i] <= 10^6

Hints:
Consider that any valid arithmetic progression will be in sorted order.
Sort the array, then check if the differences of all consecutive elements are equal.

Check Arithmetic Progression From Sequence of Numbers by Sorting

One way to check if the given sequence numbers can be re-organized into arithmetic progression is via Sorting. After all the numbers are sorted, we then can check the gaps between every two numbers. If not all gaps are the same, then the sequence cannot be formed into arithmetic progression.

The use of Sorting indicates the algorithm is O(NLogN).

C++ Code Make Arithmetic Progression

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
    bool canMakeArithmeticProgression(vector<int>& arr) {
        sort(begin(arr), end(arr));
        int diff = arr[1] - arr[0];
        for (int i = 2; i < arr.size(); ++ i) {
            if (arr[i] - arr[i - 1] != diff) {
                return false;
            }
        }
        return true;
    }
};
class Solution {
public:
    bool canMakeArithmeticProgression(vector<int>& arr) {
        sort(begin(arr), end(arr));
        int diff = arr[1] - arr[0];
        for (int i = 2; i < arr.size(); ++ i) {
            if (arr[i] - arr[i - 1] != diff) {
                return false;
            }
        }
        return true;
    }
};

Python Code Make Arithmetic Progression

1
2
3
4
5
6
7
8
class Solution:
    def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
        arr.sort()
        diff = arr[1] - arr[0]
        for i in range(1, len(arr)):
            if arr[i] - arr[i - 1] != diff:
                return False
        return True
class Solution:
    def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
        arr.sort()
        diff = arr[1] - arr[0]
        for i in range(1, len(arr)):
            if arr[i] - arr[i - 1] != diff:
                return False
        return True

Java Code Make Arithmetic Progression

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
    public boolean canMakeArithmeticProgression(int[] arr) {
        Arrays.sort(arr);
        int diff = arr[1] - arr[0];
        for (int i = 1; i < arr.length; ++ i) {
            if (arr[i] - arr[i - 1] != diff) {
                return false;
            }
        }
        return true;
    }
}
class Solution {
    public boolean canMakeArithmeticProgression(int[] arr) {
        Arrays.sort(arr);
        int diff = arr[1] - arr[0];
        for (int i = 1; i < arr.length; ++ i) {
            if (arr[i] - arr[i - 1] != diff) {
                return false;
            }
        }
        return true;
    }
}

Javascript Code Make Arithmetic Progression

The default arr.sort() in Javascript does not give a correct/expected sorting order. You have to pass a customize sorting function (lambda) e.g. (a, b) => a – b to sort the numbers in ascending order in Javascript.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * @param {number[]} arr
 * @return {boolean}
 */
var canMakeArithmeticProgression = function(arr) {
    arr.sort((a, b) => a - b);
    const diff = arr[1] - arr[0];
    for (let i = 2; i < arr.length; ++ i) {
        if (arr[i] - arr[i - 1] !== diff) {
            return false;
        }
    }
    return true;
};
/**
 * @param {number[]} arr
 * @return {boolean}
 */
var canMakeArithmeticProgression = function(arr) {
    arr.sort((a, b) => a - b);
    const diff = arr[1] - arr[0];
    for (let i = 2; i < arr.length; ++ i) {
        if (arr[i] - arr[i - 1] !== diff) {
            return false;
        }
    }
    return true;
};

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
农夫山泉危机曝出饮用水行业乱象  消费者严重不信任国产奶粉的原因  国人消费崇洋媚外导致洋奶粉的傲慢  暴利诱惑导致贴牌洋奶粉占八成市场  食药监部门曝光12个保健食品违法广告  薏米红豆汤  如何烹饪果蔬食物营养损失更少  食品安全监管常态化胜过千句狠话  夏季凉拌西红柿时用蜂蜜更滋补  对大豆制品存在的一些认识误区 
评论列表
添加评论