Date and Time: Jun 12, 2025
Link: https://leetcode.com/problems/maximum-difference-between-adjacent-elements-in-a-circular-array
| Access Time | Time | Y/N |
|---|---|---|
| Jun 12, 2025 | 8m53s | Y |
Compare the max abs difference of each pair of adjacent elements from [1, len(nums)-1]. Finally, compare the difference between the first element with the last element.
class Solution:
def maxAdjacentDistance(self, nums: List[int]) -> int:
# Q: Find max abs diff between adj elements in a circular array nums
# [0, 1, 3]
# [-1, -2, -5]
# S: Compare adj elements from [1, len(nums)-1] and finally compare the last with the first while updating ans
# TC: O(n), n=len(nums), SC: O(1)
ans = 0
for i in range(1, len(nums)):
# Update ans if greater diff exists
ans = max(ans, abs(nums[i] - nums[i-1]))
# Compare the last one and the first one
ans = max(ans, abs(nums[0] - nums[-1]))
return ansTime Complexity:
Space Complexity: