Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created June 12, 2025 21:31
Show Gist options
  • Save Ifihan/22e4437e45044592600982906fea92fb to your computer and use it in GitHub Desktop.
Save Ifihan/22e4437e45044592600982906fea92fb to your computer and use it in GitHub Desktop.
Maximum Difference Between Adjacent Elements in a Circular Array

Question

Approach

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.

Implementation

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

Complexities

  • Time: O(n)
  • Space: O(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment