|
| 1 | +<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is a multiple of 3 and a positive integer <code>k</code>.</p> |
| 2 | + |
| 3 | +<p>Divide the array <code>nums</code> into <code>n / 3</code> arrays of size <strong>3</strong> satisfying the following condition:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>The difference between <strong>any</strong> two elements in one array is <strong>less than or equal</strong> to <code>k</code>.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<p>Return a <strong>2D</strong> array containing the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return <strong>any</strong> of them.</p> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | + |
| 14 | +<div class="example-block"> |
| 15 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,3,4,8,7,9,3,5,1], k = 2</span></p> |
| 16 | + |
| 17 | +<p><strong>Output:</strong> <span class="example-io">[[1,1,3],[3,4,5],[7,8,9]]</span></p> |
| 18 | + |
| 19 | +<p><strong>Explanation:</strong></p> |
| 20 | + |
| 21 | +<p>The difference between any two elements in each array is less than or equal to 2.</p> |
| 22 | +</div> |
| 23 | + |
| 24 | +<p><strong class="example">Example 2:</strong></p> |
| 25 | + |
| 26 | +<div class="example-block"> |
| 27 | +<p><strong>Input:</strong> <span class="example-io">nums = [2,4,2,2,5,2], k = 2</span></p> |
| 28 | + |
| 29 | +<p><strong>Output:</strong> <span class="example-io">[]</span></p> |
| 30 | + |
| 31 | +<p><strong>Explanation:</strong></p> |
| 32 | + |
| 33 | +<p>Different ways to divide <code>nums</code> into 2 arrays of size 3 are:</p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li>[[2,2,2],[2,4,5]] (and its permutations)</li> |
| 37 | + <li>[[2,2,4],[2,2,5]] (and its permutations)</li> |
| 38 | +</ul> |
| 39 | + |
| 40 | +<p>Because there are four 2s there will be an array with the elements 2 and 5 no matter how we divide it. since <code>5 - 2 = 3 > k</code>, the condition is not satisfied and so there is no valid division.</p> |
| 41 | +</div> |
| 42 | + |
| 43 | +<p><strong class="example">Example 3:</strong></p> |
| 44 | + |
| 45 | +<div class="example-block"> |
| 46 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], k = 14</span></p> |
| 47 | + |
| 48 | +<p><strong>Output:</strong> <span class="example-io">[[2,2,12],[4,8,5],[5,9,7],[7,8,5],[5,9,10],[11,12,2]]</span></p> |
| 49 | + |
| 50 | +<p><strong>Explanation:</strong></p> |
| 51 | + |
| 52 | +<p>The difference between any two elements in each array is less than or equal to 14.</p> |
| 53 | +</div> |
| 54 | + |
| 55 | +<p> </p> |
| 56 | +<p><strong>Constraints:</strong></p> |
| 57 | + |
| 58 | +<ul> |
| 59 | + <li><code>n == nums.length</code></li> |
| 60 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 61 | + <li><code>n </code>is a multiple of 3</li> |
| 62 | + <li><code>1 <= nums[i] <= 10<sup>5</sup></code></li> |
| 63 | + <li><code>1 <= k <= 10<sup>5</sup></code></li> |
| 64 | +</ul> |
0 commit comments