The title
I give you an array of numbers, ARr.
A sequence in which the difference between any two adjacent terms is always equal to the same constant is called an arithmetic sequence.
Return true if the array can be rearranged to form an arithmetic sequence; Otherwise, return false.
Example 1: input: arr = [3,5,1] output: true explanation: reordering the array yields [1,3,5] or [5,3,1], where the difference between any two adjacent terms is 2 or -2, forming an arithmetic sequence. Example 2: input: arr = [1,2,4] output: false explanation: an arithmetic sequence cannot be obtained by reordering.Copy the code
Tip:
2 <= arr.length <= 1000 -10^6 <= arr[i] <= 10^6
Their thinking
class Solution: def canMakeArithmeticProgression(self, arr: List[int]) -> bool: # sort first, Sort () zipList = list(zip(arr,arr[1:])) # print(zipList) resDict = dict()# print(zipList For x,y in zipList: resDict[x-y] = 1# print(resDict) return len(resDict) == 1# if __name__ == '__main__': Ret,5,1 arr = [3] = Solution () canMakeArithmeticProgression (arr) print (ret)Copy the code