Skip to main content
Back to problems
Leetcode
Medium
Arrays
Sorting
Binary Search
Special Array With X Elements Greater Than or Equal X

Find an integer xx such that exactly xx elements in the array are greater than or equal to xx.

Acceptance 0%
Problem Statement

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 x if it exists, otherwise return -1.

Constraints

  • 1 <= nums.length
  • nums[i] are integers
  • The array size is small enough for an O(nlogn)O(n \log n) or O(n)O(n)-style solution to be appropriate
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.