> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Convert an Array Into a 2D Array With

> Tested Python solution for LeetCode 2610 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 2610, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table). [View on LeetCode](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/description/).

Generate this problem as a practice environment: tested reference solution, 23 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2610   # by problem number
lcpy gen -s convert_an_array_into_a_2d_array_with_conditions   # by problem name
```

## Problem

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.

### Examples

```
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.
```

```
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 <= 200`
* `1 <= nums[i] <= nums.length`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/convert_an_array_into_a_2d_array_with_conditions/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/convert_an_array_into_a_2d_array_with_conditions/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n)
    # Space: O(n)
    def find_matrix(self, nums: list[int]) -> list[list[int]]:
        counts = Counter(nums)
        rows: list[list[int]] = [[] for _ in range(max(counts.values()))]
        for value, count in counts.items():
            for row in rows[:count]:
                row.append(value)
        return rows
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.