[
math
sort
hash table
]
BinarySearch 0754 Arithmetic Sequence Permutation
Problem statement
https://binarysearch.com/problems/Arithmetic-Sequence-Permutation/
Solution
Sort numbers and then check that all differences between adjacent elements are equal.
Complexity
It is O(n log n)
for time and O(n)
for space.
Code
class Solution:
def solve(self, nums):
nums = sorted(nums)
return len(set(y - x for x, y in zip(nums, nums[1:]))) == 1
Remark
There is also O(n)
time complexity solution: find the smallest and the biggest values, create candidate for sequence and then check equality of counters.