Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Maximum Difference Between Adjacent Elements In A Circular Array

Find the largest absolute difference between any two neighboring elements in a circular array.

Acceptance 100%
Problem Statement

You are given a circular array of integers. In a circular array, the element after the last element is the first element.

Your task is to compute the maximum absolute difference between every pair of adjacent elements in the circle.

For each index ii, consider the pair (nums[i],nums[(i+1)modn])(nums[i], nums[(i+1) \bmod n]). Return the maximum value of nums[i]nums[(i+1)modn]|nums[i] - nums[(i+1) \bmod n]| over all indices.

Input Format

  • An integer array nums of length n.
  • The array is treated as circular, so the last element is adjacent to the first element.

Output Format

  • Return a single integer: the maximum absolute difference between adjacent elements in the circular array.

Constraints

  • 2n2 \le n
  • Values may be negative or positive integers
  • Use the circular wrap-around pair between the last and first elements
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 2, 3, 1]

Output

2

Explanation

Adjacent absolute differences are |1-2|=1, |2-3|=1, |3-1|=2, and |1-1|=0. The maximum is 2.

Example 2

Input

nums = [8, -1, 5]

Output

9

Explanation

The circular adjacent pairs are (8, -1), (-1, 5), and (5, 8). Their absolute differences are 9, 6, and 3. The maximum is 9.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.