DEV Community

Md. Rishat Talukder
Md. Rishat Talukder

Posted on

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

πŸ”Ή Problem: 2966. Divide Array Into Arrays With Max Difference

Difficulty: #Medium
Tags: #Sorting, #Greedy


πŸ“ Problem Summary

Given an array of integers nums, divide it into arrays of size 3 such that the maximum difference between the the elements in each array is at most k. Return an array of these arrays, or an empty array if it's not possible to divide the array as required.


🧠 My Thought Process

  • Brute Force Idea: The problem statement was a little confusing at first, but I realized it was about grouping numbers into sets of 3 with a maximum difference constraint.
  • Optimized Strategy: Use sorting + greedy:
  1. Sort the array to group similar numbers together.

  2. Since groups of 3 are needed, scan in chunks of 3.

  3. For each triplet: if max - min <= k, it's valid.

  4. If any triplet violates the rule, return an empty list immediately.

  • Algorithm Used: [[sorting]] [[greedy]]

βš™οΈ Code Implementation (Python)

class Solution:
    def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
        nums.sort()

        res = []

        for i in range(0,len(nums),3):
            if nums[i+2] -nums[i] <= k:
                res.append(nums[i:i+3])

            else:
                return []


        return res
Enter fullscreen mode Exit fullscreen mode

⏱️ Time & Space Complexity

  • Time: O(n log n) for sorting, O(n) for the loop, so overall O(n log n).
  • Space: O(n) for the result list, but O(1) additional space if we ignore the output.

🧩 Key Takeaways

  • βœ… What concept or trick did I learn? Sorting helps in grouping similar numbers, making it easier to check the max difference.
  • πŸ’‘ What made this problem tricky? The initial confusion about the grouping and the maximum difference constraint.
  • πŸ’­ How will I recognize a similar problem in the future? Look for problems that require grouping or partitioning with constraints, especially those involving differences or ranges.

πŸ” 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 23
Total Problems Solved 344
Confidence Today πŸ˜ƒ

Top comments (0)