3.37 Divide Array Into Arrays With Max Difference
3.37.1 Problem Metadata
- Platform: LeetCode
- Problem ID: 2966
- Difficulty: Medium
- URL: https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/
- Tags:
- Techniques: Sorting, Greedy, Array
3.37.2 Description
You are given an integer array nums of size n where n is a multiple of 3 and a positive integer k.
Divide the array nums into n / 3 arrays of size 3 satisfying the following condition:
- The difference between any two elements in one array is less than or equal to
k.
Return a 2D array containing the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
3.37.3 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]]
Explanation: The difference between any two elements in each array is less than or equal to 2.
Example 2:
Input: nums = [2,4,2,2,5,2], k = 2
Output: []
Explanation: Different ways to divide nums into 2 arrays of size 3 are:
- [[2,2,2],[2,4,5]] (and its lc-permutations)
- [[2,2,4],[2,2,5]] (and its lc-permutations)
Because there are four 2s, there will be an array with the elements 2 and 5 no matter how we divide it. The difference between 2 and 5 is 3, which is greater than k = 2.
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]]
Explanation: The difference between any two elements in each array is less than or equal to 14.
3.37.4 Constraints
n == nums.length1 <= n <= 10^5nis a multiple of31 <= nums[i] <= 10^51 <= k <= 10^5
3.37.5 Solution
3.37.5.1 Walkthrough
This solution uses a greedy sorting strategy based on a key insight: after sorting, consecutive elements have the smallest differences.
Core Strategy:
1. Sort the array to group similar values together
2. Partition into consecutive triplets - take elements at indices [0,1,2], [3,4,5], [6,7,8], etc.
3. Validate with a single check - for each triplet, only check if max - min ≤ k (i.e., nums[i*3+2] - nums[i*3] ≤ k)
Why Sorting Works:
- In a sorted triplet [a, b, c] where a ≤ b ≤ c, the maximum difference is always c - a
- All pairwise differences (b-a, c-b, c-a) are automatically ≤ k if c - a ≤ k
- Any non-consecutive grouping would only increase differences, so if consecutive grouping fails, no solution exists
Greedy Proof: If it’s possible to form valid groups at all, grouping consecutive elements after sorting is always valid. If consecutive grouping violates the constraint, no other arrangement can satisfy it because non-adjacent elements have even larger differences.
3.37.5.2 Analysis
- Time Complexity: O(n log n) - dominated by sorting
- Space Complexity: O(n) - for storing the result
3.37.5.3 Implementation Steps
- Sort the array in ascending order
- Iterate through the sorted array in steps of 3
- For each group of three consecutive elements, check if the difference between the largest and smallest exceeds
k - If any group violates the constraint (max - min > k), return an empty array
- Otherwise, add the group to the result and continue
3.37.5.4 Code - Java
class Solution {
public int[][] divideArray(int[] nums, int k) {
int len = nums.length;
Arrays.sort(nums);
int col = len / 3;
int[][] result = new int[col][3];
for(int i = 0; i < col; i++) {
//if max - min elements in the 3-pair > k, then it's impossible to satisify the conditions
if(nums[i * 3 + 2] - nums[i * 3] > k) {
return new int[][]{};
}
result[i] = new int[]{nums[i*3], nums[i*3+1], nums[i*3+2]};
}
return result;
}
}