Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
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.
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:
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.
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.
Constraints:
n == nums.length1 <= n <= 105n is a multiple of 31 <= nums[i] <= 1051 <= k <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,3,4,8,7,9,3,5,1] 2
[2,4,2,2,5,2] 2
[4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11] 14
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2966: Divide Array Into Arrays With Max Difference
class Solution {
public int[][] divideArray(int[] nums, int k) {
Arrays.sort(nums);
int n = nums.length;
int[][] ans = new int[n / 3][];
for (int i = 0; i < n; i += 3) {
int[] t = Arrays.copyOfRange(nums, i, i + 3);
if (t[2] - t[0] > k) {
return new int[][] {};
}
ans[i / 3] = t;
}
return ans;
}
}
// Accepted solution for LeetCode #2966: Divide Array Into Arrays With Max Difference
func divideArray(nums []int, k int) [][]int {
sort.Ints(nums)
ans := [][]int{}
for i := 0; i < len(nums); i += 3 {
t := slices.Clone(nums[i : i+3])
if t[2]-t[0] > k {
return [][]int{}
}
ans = append(ans, t)
}
return ans
}
# Accepted solution for LeetCode #2966: Divide Array Into Arrays With Max Difference
class Solution:
def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
nums.sort()
ans = []
n = len(nums)
for i in range(0, n, 3):
t = nums[i : i + 3]
if t[2] - t[0] > k:
return []
ans.append(t)
return ans
// Accepted solution for LeetCode #2966: Divide Array Into Arrays With Max Difference
impl Solution {
pub fn divide_array(mut nums: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
nums.sort();
let mut ans = Vec::new();
let n = nums.len();
for i in (0..n).step_by(3) {
if i + 2 >= n {
return vec![];
}
let t = &nums[i..i + 3];
if t[2] - t[0] > k {
return vec![];
}
ans.push(t.to_vec());
}
ans
}
}
// Accepted solution for LeetCode #2966: Divide Array Into Arrays With Max Difference
function divideArray(nums: number[], k: number): number[][] {
nums.sort((a, b) => a - b);
const ans: number[][] = [];
for (let i = 0; i < nums.length; i += 3) {
const t = nums.slice(i, i + 3);
if (t[2] - t[0] > k) {
return [];
}
ans.push(t);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.