Maximum Difference Between Adjacent Elements in a Circular Array - Problem
Given a circular array nums, find the maximum absolute difference between adjacent elements.
Note: In a circular array, the first and last elements are adjacent.
For example, if nums = [1, 5, 3, 9], the adjacent pairs are: (1,5), (5,3), (3,9), and (9,1).
Input & Output
Example 1 — Basic Circular Array
$
Input:
nums = [1, 5, 3, 9]
›
Output:
8
💡 Note:
Adjacent pairs are: (1,5)→4, (5,3)→2, (3,9)→6, (9,1)→8. Maximum difference is 8 between last and first elements.
Example 2 — Small Array
$
Input:
nums = [2, 7]
›
Output:
5
💡 Note:
Only one adjacent pair: (2,7) and (7,2), both have difference |2-7| = 5.
Example 3 — All Same Elements
$
Input:
nums = [5, 5, 5, 5]
›
Output:
0
💡 Note:
All adjacent differences are |5-5| = 0, so maximum is 0.
Constraints
- 2 ≤ nums.length ≤ 104
- -109 ≤ nums[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code