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

# Special Array With X Elements Greater Than or

> Tested Python solution for LeetCode 1608 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1608, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/description/).

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

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

## Problem

You are given an array `nums` of non-negative integers. `nums` is considered **special** if there exists a number `x` such that there are **exactly** `x` numbers in `nums` that are **greater than or equal to** `x`.

Notice that `x` **does not** have to be an element in `nums`.

Return `x` *if the array is* ***special***, *otherwise, return* `-1`. It can be proven that if `nums` is special, the value for `x` is **unique**.

### Examples

```
Input: nums = [3,5]
Output: 2
Explanation: There are 2 values (3 and 5) that are greater than or equal to 2.
```

```
Input: nums = [0,0]
Output: -1
Explanation: No numbers fit the criteria for x.
If x = 0, there should be 0 numbers >= x, but there are 2.
If x = 1, there should be 1 number >= x, but there are 0.
If x = 2, there should be 2 numbers >= x, but there are 0.
x cannot be greater since there are only 2 numbers in nums.
```

```
Input: nums = [0,4,3,0,4]
Output: 3
Explanation: There are 3 values that are greater than or equal to 3.
```

### Constraints

* `1 <= nums.length <= 100`
* `0 <= nums[i] <= 1000`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m) where m = max(nums)
    # Space: O(m)
    def special_array(self, nums: list[int]) -> int:
        n = len(nums)
        counts = [0] * (n + 1)
        for num in nums:
            counts[min(num, n)] += 1
        total = 0
        for x in range(n, -1, -1):
            total += counts[x]
            if total == x:
                return x
        return -1
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) where m = max(nums) | O(m) |

## Tags

[NeetCode All](/catalog/neetcode).


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