DEV Community

Md. Rishat Talukder
Md. Rishat Talukder

Posted on

🧠 Solving LeetCode Until I Become Top 1% β€” Day `17`

πŸ”Ή Problem: 3423 Maximum Difference Between Adjacent Elements in a Circular Array

Difficulty: #Easy
Tags: #Array


πŸ“ Problem Summary (in your own words)

Given a circular array, find the maximum absolute difference between adjacent elements. The array wraps around, meaning the last element is adjacent to the first.


🧠 My Thought Process

  • Brute Force Idea:
    Well finally a problem that I can solve without looking at the solution and cofirming that I am on the right track. The problem has a word "circular" in it and don't get confused by it. You can think that it might be a linked list or something, but it's just a normal array. Just the first and last elements are adjacent to each other. So, we can just iterate through the array and find the maximum absolute difference between adjacent elements. You can either find the absolute difference between the first and last elements separately or just add it to the end of the array and iterate through it. I think both approaches are valid.

  • Optimized Strategy:

    • Step 1: Add the first element to the end of the array to handle the circular nature.
    • Step 2: Initialize a variable result to store the maximum absolute difference.
    • Step 3: Iterate through the array and calculate the absolute difference between each adjacent pair of elements.
    • Step 4: Update result if the current absolute difference is greater than the previous maximum.
  • Step 5: Return the result as the final answer.


βš™οΈ Code Implementation (Python)

class Solution:
    def maxAdjacentDistance(self, nums: List[int]) -> int:
        nums.append(nums[0])
        res = float('-inf')
        for i in range(1,len(nums)):
            res = max(res, abs(nums[i]-nums[i-1]))

        return res
Enter fullscreen mode Exit fullscreen mode

⏱️ Time & Space Complexity

  • Time: O(n)
    • We iterate through the array once to calculate the maximum absolute difference.
  • Space: O(1)
    • We use a constant amount of space for the result variable and do not use any additional data structures that scale with input size.

🧩 Key Takeaways

  • βœ… What concept or trick did I learn?
    • Nothing new.
  • πŸ’‘ What made this problem tricky?
    • For beginners the word "circular" might be confusing, but it's just a normal array with the first and last elements being adjacent.
  • πŸ’­ How will I recognize a similar problem in the future?
    • Look for the word "circular" in the problem statement.

πŸ” Reflection (Self-Check)

  • [βœ…] Could I solve this without help?
  • [βœ…] Did I write code from scratch?
  • [βœ…] Did I understand why it works?
  • [βœ…] Will I be able to recall this in a week?

πŸš€ Progress Tracker

Metric Value
Day 17
Total Problems Solved 349
Confidence Today πŸ˜ƒ

Top comments (1)

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