2966. Divide Array Into Arrays With Max Difference Medium
1/**
2 * [2966] Divide Array Into Arrays With Max Difference
3 *
4 * You are given an integer array nums of size n where n is a multiple of 3 and a positive integer k.
5 * Divide the array nums into n / 3 arrays of size 3 satisfying the following condition:
6 *
7 * The difference between any two elements in one array is less than or equal to k.
8 *
9 * 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.
10 *
11 * <strong class="example">Example 1:
12 * <div class="example-block">
13 * Input: <span class="example-io">nums = [1,3,4,8,7,9,3,5,1], k = 2</span>
14 * Output: <span class="example-io">[[1,1,3],[3,4,5],[7,8,9]]</span>
15 * Explanation:
16 * The difference between any two elements in each array is less than or equal to 2.
17 * </div>
18 * <strong class="example">Example 2:
19 * <div class="example-block">
20 * Input: <span class="example-io">nums = [2,4,2,2,5,2], k = 2</span>
21 * Output: <span class="example-io">[]</span>
22 * Explanation:
23 * Different ways to divide nums into 2 arrays of size 3 are:
24 *
25 * [[2,2,2],[2,4,5]] (and its permutations)
26 * [[2,2,4],[2,2,5]] (and its permutations)
27 *
28 * 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.
29 * </div>
30 * <strong class="example">Example 3:
31 * <div class="example-block">
32 * Input: <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>
33 * Output: <span class="example-io">[[2,2,12],[4,8,5],[5,9,7],[7,8,5],[5,9,10],[11,12,2]]</span>
34 * Explanation:
35 * The difference between any two elements in each array is less than or equal to 14.
36 * </div>
37 *
38 * Constraints:
39 *
40 * n == nums.length
41 * 1 <= n <= 10^5
42 * n is a multiple of 3
43 * 1 <= nums[i] <= 10^5
44 * 1 <= k <= 10^5
45 *
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/
50// discuss: https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55 pub fn divide_array(nums: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
56 vec![]
57 }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_2966() {
68 }
69}
70
Back
© 2025 bowen.ge All Rights Reserved.