Find an integer such that exactly elements in the array are greater than or equal to .
Problem
Given an integer array nums, determine whether there exists an integer x such that exactly x elements in nums are greater than or equal to x.
Return the special value x if it exists. If more than one value could work, return the one that satisfies the condition. If no such value exists, return -1.
A valid solution should inspect the array efficiently rather than checking every possible value naively.
Notes
- Elements may be repeated.
- The special value, if it exists, is an integer.
- You only need to determine one valid answer.
Input Format
- An integer array
nums.
Output Format
- Return the special integer
xif it exists, otherwise return-1.
Constraints
1 <= nums.lengthnums[i]are integers- The array size is small enough for an or -style solution to be appropriate
Example 1
Input
nums = [3,5]
Output
2
Explanation
There are exactly 2 elements greater than or equal to 2, so 2 is special.
Example 2
Input
nums = [0,0]
Output
-1
Explanation
No integer x satisfies the condition.
Show 1 more example
Example 3
Input
nums = [0,4,3,0,4]
Output
3
Explanation
Exactly 3 elements are greater than or equal to 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.