Maximum Difference Between Adjacent Elements in a Circular Array - Solution & Explanation
Problem Statement
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.
Example 1:
Input: nums = [1,2,4]
Output: 3
Explanation:
Because nums is circular, nums[0] and nums[2] are adjacent. They have the maximum absolute difference of |4 - 1| = 3.
Example 2:
Input: nums = [-5,-10,-5]
Output: 5
Explanation:
The adjacent elements nums[0] and nums[1] have the maximum absolute difference of |-5 - (-10)| = 5.
Constraints:
2 <= nums.length <= 100-100 <= nums[i] <= 100
Approach Overview
Problem Overview: Given an integer array, you need the maximum absolute difference between two adjacent elements. The array is circular, meaning the last element is also adjacent to the first. Your job is to check every adjacent pair, including the wrap‑around pair, and return the largest absolute difference.
Approach 1: Brute Force Pair Comparison (O(n) time, O(1) space)
The straightforward idea is to examine every adjacent pair and compute abs(nums[i] - nums[i+1]). For the circular condition, you also compare the last element with the first element. Track the maximum difference while iterating. Even though this is called "brute force", it already runs in linear time because each element participates in a constant number of comparisons. This approach works well when the array is small or when clarity matters more than micro‑optimizations. The only data structure needed is the input array, and you maintain a running maximum while iterating.
Approach 2: Circular Simulation with Modulo Indexing (O(n) time, O(1) space)
A cleaner way to handle circular adjacency is to treat the array as a loop. Iterate through indices 0 to n-1, and compute the next index using (i + 1) % n. This guarantees that when i reaches the last element, the next index wraps back to 0. For each step, calculate abs(nums[i] - nums[(i + 1) % n]) and update the maximum difference. This approach avoids special handling for the last element and keeps the logic uniform. The algorithm is a classic array traversal combined with a simple simulation of circular behavior.
Why this works: Every adjacent pair appears exactly once in the traversal. The modulo operation ensures the circular pair (last → first) is included without extra branching logic. Since each element is processed once and only constant extra variables are used, the algorithm runs in O(n) time and O(1) space.
Recommended for interviews: Interviewers typically expect the single‑pass circular traversal using modulo indexing. It demonstrates that you recognized the circular property and handled it cleanly without special cases. Showing the simple pair comparison idea first proves you understand the problem. Converting it into a concise O(n) loop with (i + 1) % n shows solid implementation skill.
Solution
We traverse the array nums, calculate the absolute difference between adjacent elements, and maintain the maximum absolute difference. Finally, we compare it with the absolute difference between the first and last elements and take the maximum value.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Adjacent Comparison | O(n) | O(1) | When implementing the simplest readable solution with explicit handling of the last and first element. |
| Circular Simulation with Modulo | O(n) | O(1) | Preferred approach in interviews; cleanly handles circular adjacency using modulo indexing. |
Video Solution
3423. Maximum Difference Between Adjacent Elements in a Circular Array - Day 12/30 Leetcode June • Programming Live with Larry • 334 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Maximum Difference Between Adjacent Elements in a Circular Array easy or hard?
Maximum Difference Between Adjacent Elements in a Circular Array Python/Java solution
How to solve Maximum Difference Between Adjacent Elements in a Circular Array in O(n)?
What is the best approach for Maximum Difference Between Adjacent Elements in a Circular Array?
Is Maximum Difference Between Adjacent Elements in a Circular Array asked at Google/Amazon/Meta?
What data structure is used in Maximum Difference Between Adjacent Elements in a Circular Array?
What is the time complexity of Maximum Difference Between Adjacent Elements in a Circular Array?
Ready to solve this problem?
Practice Maximum Difference Between Adjacent Elements in a Circular Array with our built-in code editor and test cases.
Practice on FleetCode