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

# Minimum Number of Operations to Make (#2870)

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

LeetCode 2870, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Greedy](/catalog/topics/greedy), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/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 2870   # by problem number
lcpy gen -s minimum_number_of_operations_to_make_array_empty   # by problem name
```

## Problem

\<p>You are given a \<strong>0-indexed\</strong> array \<code>nums\</code> consisting of positive integers.\</p>

\<p>There are two types of operations that you can apply on the array \<strong>any\</strong> number of times:\</p>

\<ul>
\<li>Choose \<strong>two\</strong> elements with \<strong>equal\</strong> values and \<strong>delete\</strong> them from the array.\</li>
\<li>Choose \<strong>three\</strong> elements with \<strong>equal\</strong> values and \<strong>delete\</strong> them from the array.\</li>
\</ul>

\<p>Return \<em>the \<strong>minimum\</strong> number of operations required to make the array empty, or \</em>\<code>-1\</code>\<em> if it is not possible\</em>.\</p>

### Examples

```
Input: nums = [2,3,3,2,2,4,2,3,4]
Output: 4
Explanation: We can apply the following operations to make the array empty:
- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
It can be shown that we cannot make the array empty in less than 4 operations.
```

```
Input: nums = [2,1,2,2,3,3]
Output: -1
Explanation: It is impossible to empty the array.
```

### Constraints

* 2 \<= nums.length \<= 10^5
* 1 \<= nums\[i] \<= 10^6

**Note:** This question is the same as 2244: Minimum Rounds to Complete All Tasks.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_operations_to_make_array_empty/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_operations_to_make_array_empty/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 min_operations(self, nums: list[int]) -> int:
        operations = 0
        for count in Counter(nums).values():
            if count == 1:
                return -1
            groups, remainder = divmod(count, 3)
            operations += groups + (1 if remainder else 0)
        return operations
```

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