Skip to main content
LeetCode 2870, Medium. Topics: Array, Hash Table, Greedy, Counting. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 23 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026