Skip to main content

Maximum Difference Between Adjacent Elements in a Circular Array - Solution & Explanation

EasyArray6 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

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).

Code

Python

Java

C++

Go

TypeScript

Rust

C#

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Adjacent ComparisonO(n)O(1)When implementing the simplest readable solution with explicit handling of the last and first element.
Circular Simulation with ModuloO(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 is classified as an Easy problem. The core challenge is recognizing the circular adjacency and ensuring the last element is compared with the first during traversal.
Maximum Difference Between Adjacent Elements in a Circular Array Python/Java solution
Most implementations follow the same logic across languages: iterate through the array and compute abs(nums[i] - nums[(i + 1) % n]). Python, Java, C++, Go, TypeScript, Rust, and C# implementations all run in O(n) time and O(1) space using this pattern.
How to solve Maximum Difference Between Adjacent Elements in a Circular Array in O(n)?
Traverse the array once and compute the absolute difference between nums[i] and nums[(i + 1) % n]. The modulo ensures the last element is compared with the first, preserving the circular property. Update a running maximum during the loop and return it after the traversal.
What is the best approach for Maximum Difference Between Adjacent Elements in a Circular Array?
The best approach is a single-pass simulation that checks each pair of adjacent elements using modulo indexing. Iterate from index 0 to n-1 and compute abs(nums[i] - nums[(i + 1) % n]). This automatically includes the circular pair between the last and first elements. The algorithm runs in O(n) time with O(1) extra space.
Is Maximum Difference Between Adjacent Elements in a Circular Array asked at Google/Amazon/Meta?
This style of array traversal problem commonly appears in coding interviews at large tech companies including Amazon, Google, and Meta. While the exact problem ID may vary, handling circular arrays and computing adjacent relationships is a frequent interview pattern.
What data structure is used in Maximum Difference Between Adjacent Elements in a Circular Array?
The problem primarily uses a basic array with sequential traversal. No additional data structures are required since the solution only tracks a running maximum and compares adjacent values.
What is the time complexity of Maximum Difference Between Adjacent Elements in a Circular Array?
The optimal solution runs in O(n) time because each element is compared with its next neighbor exactly once. Only constant extra variables are used to track the maximum difference, so the space complexity is O(1).

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