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. You need to create a 2D array from nums satisfying the following conditions:
nums.Return the resulting array. If there are multiple answers, return any of them.
Note that the 2D array can have a different number of elements on each row.
Example 1:
Input: nums = [1,3,4,1,2,3,1] Output: [[1,3,4,2],[1,3],[1]] Explanation: We can create a 2D array that contains the following rows: - 1,3,4,2 - 1,3 - 1 All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer. It can be shown that we cannot have less than 3 rows in a valid array.
Example 2:
Input: nums = [1,2,3,4] Output: [[4,3,2,1]] Explanation: All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.
Constraints:
1 <= nums.length <= 2001 <= nums[i] <= nums.lengthProblem summary: You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions: The 2D array should contain only the elements of the array nums. Each row in the 2D array contains distinct integers. The number of rows in the 2D array should be minimal. Return the resulting array. If there are multiple answers, return any of them. Note that the 2D array can have a different number of elements on each row.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,3,4,1,2,3,1]
[2,1,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2610: Convert an Array Into a 2D Array With Conditions
class Solution {
public List<List<Integer>> findMatrix(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
int n = nums.length;
int[] cnt = new int[n + 1];
for (int x : nums) {
++cnt[x];
}
for (int x = 1; x <= n; ++x) {
int v = cnt[x];
for (int j = 0; j < v; ++j) {
if (ans.size() <= j) {
ans.add(new ArrayList<>());
}
ans.get(j).add(x);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2610: Convert an Array Into a 2D Array With Conditions
func findMatrix(nums []int) (ans [][]int) {
n := len(nums)
cnt := make([]int, n+1)
for _, x := range nums {
cnt[x]++
}
for x, v := range cnt {
for j := 0; j < v; j++ {
if len(ans) <= j {
ans = append(ans, []int{})
}
ans[j] = append(ans[j], x)
}
}
return
}
# Accepted solution for LeetCode #2610: Convert an Array Into a 2D Array With Conditions
class Solution:
def findMatrix(self, nums: List[int]) -> List[List[int]]:
cnt = Counter(nums)
ans = []
for x, v in cnt.items():
for i in range(v):
if len(ans) <= i:
ans.append([])
ans[i].append(x)
return ans
// Accepted solution for LeetCode #2610: Convert an Array Into a 2D Array With Conditions
impl Solution {
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
let n = nums.len();
let mut cnt = vec![0; n + 1];
let mut ans: Vec<Vec<i32>> = Vec::new();
for &x in &nums {
cnt[x as usize] += 1;
}
for x in 1..=n as i32 {
for j in 0..cnt[x as usize] {
if ans.len() <= j {
ans.push(Vec::new());
}
ans[j].push(x);
}
}
ans
}
}
// Accepted solution for LeetCode #2610: Convert an Array Into a 2D Array With Conditions
function findMatrix(nums: number[]): number[][] {
const ans: number[][] = [];
const n = nums.length;
const cnt: number[] = Array(n + 1).fill(0);
for (const x of nums) {
++cnt[x];
}
for (let x = 1; x <= n; ++x) {
for (let j = 0; j < cnt[x]; ++j) {
if (ans.length <= j) {
ans.push([]);
}
ans[j].push(x);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.