Special Array With X Elements >= X

EasyArrayMath

Description

You are given an array nums of non-negative integers. nums is special if there exists a number x such that exactly x numbers in nums are >= x. Return x or -1.

Examples

Input:nums = [3,5]
Output:2
Explanation:

Both 3 and 5 are >= 2, giving exactly 2 elements >= 2. So x=2 satisfies the special condition.

Input:nums = [0,0,4,4,4]
Output:3
Explanation:

There are exactly 3 elements that are >= 3 (the three 4s), so x = 3

Input:nums = [1,2,3,4,5,6]
Output:-1
Explanation:

No value x has exactly x elements greater than or equal to x, so the answer is -1.

Constraints

  • 1 ≤ nums.length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!