Since the array is circular, I iterate through each element and compute the absolute difference between every pair of adjacent elements, including the last and the first. I keep track of the maximum difference encountered during this iteration.
class Solution:
def maxAdjacentDistance(self, nums: List[int]) -> int:
n = len(nums)
max_diff = 0
for i in range(n):
diff = abs(nums[i] - nums[(i + 1) % n])
max_diff = max(max_diff, diff)
return max_diff
- Time: O(n)
- Space: O(1)