Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,4]
[-5,-10,-5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3423: Maximum Difference Between Adjacent Elements in a Circular Array
class Solution {
public int maxAdjacentDistance(int[] nums) {
int n = nums.length;
int ans = Math.abs(nums[0] - nums[n - 1]);
for (int i = 1; i < n; ++i) {
ans = Math.max(ans, Math.abs(nums[i] - nums[i - 1]));
}
return ans;
}
}
// Accepted solution for LeetCode #3423: Maximum Difference Between Adjacent Elements in a Circular Array
func maxAdjacentDistance(nums []int) int {
ans := abs(nums[0] - nums[len(nums)-1])
for i := 1; i < len(nums); i++ {
ans = max(ans, abs(nums[i]-nums[i-1]))
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #3423: Maximum Difference Between Adjacent Elements in a Circular Array
class Solution:
def maxAdjacentDistance(self, nums: List[int]) -> int:
return max(abs(a - b) for a, b in pairwise(nums + [nums[0]]))
// Accepted solution for LeetCode #3423: Maximum Difference Between Adjacent Elements in a Circular Array
impl Solution {
pub fn max_adjacent_distance(nums: Vec<i32>) -> i32 {
nums.iter()
.zip(nums.iter().cycle().skip(1))
.take(nums.len())
.map(|(a, b)| (*a - *b).abs())
.max()
.unwrap_or(0)
}
}
// Accepted solution for LeetCode #3423: Maximum Difference Between Adjacent Elements in a Circular Array
function maxAdjacentDistance(nums: number[]): number {
const n = nums.length;
let ans = Math.abs(nums[0] - nums[n - 1]);
for (let i = 1; i < n; ++i) {
ans = Math.max(ans, Math.abs(nums[i] - nums[i - 1]));
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.