Skip to content

2966. Divide Array Into Arrays With Max Difference #1822

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to divide an integer array into multiple arrays of size 3 such that the difference between any two elements in each sub-array is less than or equal to a given integer k. If it's impossible to satisfy the conditions, we return an empty array.

Approach

  1. Sorting the Array: The first step is to sort the given array. Sorting helps in systematically grouping elements into triplets where the difference between the smallest and largest elements in each triplet can be easily checked.
  2. Consecutive Triplet Check: After sorting, we iterate through the array in steps of three. For each group of three consecutive elements (starting from the smallest), we check if the difference between the third…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jun 18, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants