Problem statement

https://binarysearch.com/problems/K-Numbers-Greater-Than-or-Equal-to-K/

Solution

Equal to Leetcode 1608. Special Array With X Elements Greater Than or Equal X.

Complexity

It is O(n log n) for time and O(n) for space.

Code

class Solution:
    def solve(self, A):
        A, n, i = sorted(A)[::-1], len(A), 0
        while i < n and A[i] > i: i += 1
        return -1 if i < n and i == A[i] else i