Find an integer such that exactly elements in the array are greater than or equal to .
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.
nums.x if it exists, otherwise return -1.1 <= nums.lengthnums[i] are integersExample 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.
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
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.