DEV Community

Cover image for 🎯"Maximum Difference Between Adjacent Elements in a Circular Array" LeetCode 3423 (C++ | JavaScript | Python)
Om Shree
Om Shree

Posted on

🎯"Maximum Difference Between Adjacent Elements in a Circular Array" LeetCode 3423 (C++ | JavaScript | Python)

Hey problem solvers! 🎯

Today we’re tackling a fun and elegant problem from LeetCode β€” 3423: Maximum Difference Between Adjacent Elements in a Circular Array. This one checks your understanding of array boundaries and circular behavior. Let’s break it down. πŸ”


πŸ“œ Problem Overview

You're given a circular array nums, and your task is to find the maximum absolute difference between adjacent elements, where:

  • The first and last elements are also considered adjacent (because it's circular).
  • You return the maximum absolute difference between any two adjacent numbers.

✨ Example

Input: nums = [1, 2, 4]
Output: 3
Explanation:
Adjacent pairs: |1 - 2| = 1, |2 - 4| = 2, |4 - 1| = 3.
Max is 3.


🧠 Intuition

The array is circular, so both regular adjacent pairs and the pair between the last and first element must be considered.

Strategy:

  • Initialize ans with |nums[0] - nums[n-1]|.
  • Iterate through the array and compute |nums[i] - nums[i+1]| for all valid i.
  • Return the maximum of all these values.

βš™οΈ C++ Code

class Solution {
 public:
  int maxAdjacentDistance(vector<int>& nums) {
    int ans = abs(nums.front() - nums.back());

    for (int i = 0; i + 1 < nums.size(); ++i)
      ans = max(ans, abs(nums[i] - nums[i + 1]));

    return ans;
  }
};
Enter fullscreen mode Exit fullscreen mode

🌐 JavaScript Version

var maxAdjacentDistance = function(nums) {
    let ans = Math.abs(nums[0] - nums[nums.length - 1]);

    for (let i = 0; i + 1 < nums.length; ++i) {
        ans = Math.max(ans, Math.abs(nums[i] - nums[i + 1]));
    }

    return ans;
};
Enter fullscreen mode Exit fullscreen mode

🐍 Python Version

def maxAdjacentDistance(nums):
    ans = abs(nums[0] - nums[-1])

    for i in range(len(nums) - 1):
        ans = max(ans, abs(nums[i] - nums[i + 1]))

    return ans
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Test Cases

Input: nums = [1, 2, 4]
Output: 3

Input: nums = [-5, -10, -5]
Output: 5

Input: nums = [0, 100, -100]
Output: 200
Enter fullscreen mode Exit fullscreen mode

⏱ Time & Space Complexity

Time Complexity: O(n) β€” one pass through the array
Space Complexity: O(1) β€” constant extra space
Enter fullscreen mode Exit fullscreen mode

βœ… Wrap-Up

This is a great warm-up problem to train your attention to edge conditions like circular connections. Simple logic and clean implementation go a long way here.

If this guide helped you, drop a ❀️ and follow for more beginner-friendly coding guides!

Happy coding! πŸš€

Top comments (3)

Collapse
 
thedeepseeker profile image
Anna kowoski

Well Explained

Collapse
 
om_shree_0709 profile image
Om Shree

Thanks Anna!!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.