Can make arithmetic progression from sequence
June 5, 2023
math-and-geometry array-and-hashmapProblem URL: Can make arithmetic progression from sequence
We will sort the array and then check if the difference between each element is the same.
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
Time complexity: O(nlog(n))
Space complexity: O(1)