549. Special Array With X Elements Greater Than or Equal to X

HardBinary SearchBinary SearchArray

An array is special if there exists a number x such that exactly x elements of the array are greater than or equal to x (x need not be an array element). Return that x if it exists, otherwise -1. The value x is unique when it exists. Input: an array of non-negative integers.

Input: An array of non-negative integers.

Output: Integer — the value x, or -1.

Examples

Example 1
Input: [0,4,3,0,4]
Output: 3
Explanation: Exactly 3 elements are >= 3.
Example 2
Input: [3,5]
Output: 2
Explanation: Two elements are >= 2.
Example 3
Input: [0,0]
Output: -1
Explanation: No valid x.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →