Convert an Array Into a 2D Array With Conditions - Problem
Transform Array Into Optimal 2D Matrix

You're given an integer array nums and need to reorganize it into a 2D array following specific rules:

๐Ÿ“‹ Requirements:
  • Use all elements from the original array
  • Each row must contain distinct integers (no duplicates within a row)
  • Minimize the number of rows in the result

Your task is to return the resulting 2D array. Multiple valid answers may exist - return any of them.

๐Ÿ’ก Key Insight: The minimum number of rows equals the frequency of the most common element!

Example: [1,3,4,1,2,3,1] โ†’ [[1,3,4,2],[1,3],[1]]
Since 1 appears 3 times, we need at least 3 rows.

Input & Output

example_1.py โ€” Python
$ Input: [1,3,4,1,2,3,1]
โ€บ Output: [[1,3,4,2],[1,3],[1]]
๐Ÿ’ก Note: Element 1 appears 3 times, so we need at least 3 rows. We distribute: first row gets one occurrence of each element [1,3,4,2], second row gets remaining occurrences [1,3], third row gets the final [1].
example_2.py โ€” Python
$ Input: [1,2,3,4]
โ€บ Output: [[1,2,3,4]]
๐Ÿ’ก Note: All elements are unique (frequency = 1), so we only need one row containing all elements.
example_3.py โ€” Python
$ Input: [1,1,1,1]
โ€บ Output: [[1],[1],[1],[1]]
๐Ÿ’ก Note: Element 1 appears 4 times, and each row can only contain distinct elements, so we need 4 rows with one element each.

Visualization

Tap to expand
๐ŸŽฏ Restaurant Seating AnalogyCustomers: [Badge 1, Badge 3, Badge 4, Badge 1, Badge 2, Badge 3, Badge 1]Step 1: Count Badge Types1ร—33ร—24ร—12ร—1Step 2: Badge 1 appears 3 times โ†’ Need 3 TablesTable 1Table 2Table 3Step 3: Seat Customers (Round-Robin)Table 11342Table 213Table 31Result: [[1,3,4,2], [1,3], [1]] - No duplicate badges at any table!
Understanding the Visualization
1
Count Customer Types
Count how many customers have each badge number
2
Determine Tables Needed
The badge number that appears most frequently determines minimum tables needed
3
Seat Customers
Use round-robin to seat customers - place one of each badge type at each table
4
Final Arrangement
All customers are seated with no duplicate badges at any table
Key Takeaway
๐ŸŽฏ Key Insight: The frequency of the most common element determines the minimum number of rows needed, and round-robin distribution ensures optimal placement.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass to count frequencies, single pass to place elements

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Where k is number of unique elements, for the frequency map

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 200
  • 1 โ‰ค nums[i] โ‰ค nums.length
  • Each row must contain distinct integers
  • Minimize the number of rows in the result
Asked in
Amazon 35 Google 28 Meta 22 Microsoft 18
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen