> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Divide Array Into Arrays With Max Difference

> Tested Python solution for LeetCode 2966 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2966, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2966   # by problem number
lcpy gen -s divide_array_into_arrays_with_max_difference   # by problem name
```

## Problem

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.

### Examples

```
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.
```

```
Input: nums = [2,4,2,2,5,2], k = 2
Output: []
Explanation: Because there are four 2s there will be an array with the elements 2 and 5 no matter how we divide it. Since 5 - 2 = 3 > k, the condition is not satisfied and so there is no valid division.
```

```
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.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/divide_array_into_arrays_with_max_difference/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/divide_array_into_arrays_with_max_difference/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n) for the sorted copy and the output
    def divide_array(self, nums: list[int], k: int) -> list[list[int]]:
        nums_sorted = sorted(nums)
        result: list[list[int]] = []
        for i in range(0, len(nums_sorted), 3):
            group = nums_sorted[i : i + 3]
            if group[2] - group[0] > k:
                return []
            result.append(group)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) for the sorted copy and the output |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.