Skip to content

Practice Divide Array Into Arrays With Max Difference

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2966
LeetCode Problem

Divide Array Into Arrays With Max Difference

Problem Statement

nums.length is a multiple of three. Partition all entries into groups of exactly three, with each group's maximum minus minimum at most k. Return any valid grouping, or [] if no valid grouping exists.

Source: Divide Array Into Arrays With Max Difference on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,3,4,8,7,9,3,5,1], k = 2
Output
[[1,1,3],[3,4,5],[7,8,9]]

# Example 2

Input
nums = [2,4,2,2,5,2], k = 2
Output
[]

# Example 3

Input
nums = [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], k = 14
Output
[[2,2,2],[4,5,5],[5,5,7],[7,8,8],[9,9,10],[11,12,12]]

Constraints

n == nums.length
1 <= n <= 10^5
n is a multiple of 3
1 <= nums[i] <= 10^5
1 <= k <= 10^5

Related LeetCode Problems

Practice more problems similar to Divide Array Into Arrays With Max Difference.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.