Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Special Array With X Elements Greater Than or Equal X

XPChallenge Points: 10
levelLevel: Easy

You are given an array nums containing non-negative integers.
We say the array is special if there exists a number x such that exactly x elements in the array have values greater than or equal to x.

Note that x is not required to appear inside the array itself.

Your task is to determine this value x.
If such an x exists, return it.
If no number satisfies the condition, return -1.

It is guaranteed that if a valid x exists, it will be unique.

Example 1:

Input: nums = [1, 2, 3, 4]

Output: 3

Explanation:

There are exactly 2 elements (3,4) that are ≥ 2.

Example 2:

Input: nums = [5, 0, 0, 5, 6]

Output: -1

Explanation:

Three values (5,5,6) are ≥ 3.

Example 3:

Input: nums = [0,1,1]

Output: -1

Explanation:

No value x satisfies “exactly x elements ≥ x”.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result