Special Array With X Elements >= X

Easy

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:

2 elements >= 2.

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:4
Explanation:

There are exactly 4 elements that are >= 4 (4, 5, 6, and one more), so x = 4

Constraints

  • 1 ≤ nums.length ≤ 100

Ready to solve this problem?

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