Skip to content

Latest commit

 

History

History
42 lines (32 loc) · 2.16 KB

File metadata and controls

42 lines (32 loc) · 2.16 KB

3423. Maximum Difference Between Adjacent Elements in a Circular Array (Easy)

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

Walk-through:

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.


Python:

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 ans

Time Complexity: $O(n)$
Space Complexity: $O(1)$


CC BY-NC-SABY: credit must be given to the creatorNC: Only noncommercial uses of the work are permittedSA: Adaptations must be shared under the same terms