Special Array With X Elements Greater Than or Equal X (LeetCode 1608): Solution and Explanation

Easy
Array
Binary Search
Sorting

Find the unique x such that exactly x elements of nums are at least x, or return -1 if no such x exists.

Intuition

x is a count of elements, so it can never exceed n, the length of the array. That leaves only n + 1 candidates, 0 through n.

Let count(x) be how many elements are at least x. Raising x can only shrink that set, so count(x) never increases while x itself goes up by one each step. The difference count(x) - x therefore strictly decreases, which also explains why the answer is unique.

A strictly decreasing function crosses zero at most once, so binary search can find it. If count(x) is larger than x, the crossing is to the right. If it is smaller, the crossing is to the left.

Approach: Binary search on x

  1. Set lo = 0 and hi = n.
  2. While lo <= hi, take x = (lo + hi) / 2 and count the elements >= x with one pass.
  3. If the count equals x, return x.
  4. If the count is larger than x, every smaller x has an even larger count, so set lo = x + 1.
  5. Otherwise set hi = x - 1.
  6. If the range empties, return -1.

Step-by-step example

nums = [3,5]
  1. lo = 0, hi = 2.
  2. x = 1: both 3 and 5 are >= 1, count 2 > 1, so lo = 2.
  3. x = 2: both are >= 2, count 2 = x. Return 2.

Code: Python, Java, C++

1class Solution:
2    def specialArray(self, nums: List[int]) -> int:
3        lo, hi = 0, len(nums)
4        while lo <= hi:
5            x = (lo + hi) // 2
6            count = 0
7            for v in nums:
8                if v >= x:
9                    count += 1
10            if count == x:
11                return x
12            if count > x:
13                lo = x + 1
14            else:
15                hi = x - 1
16        return -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Counting buckets O(n)
Binary search on x O(n log n)
Sort, then scan O(n log n)
Time

O(n log n)

The binary search runs O(log n) rounds over the range [0, n], and each round counts with one O(n) pass.

Space

O(1)

Only the bounds, the candidate and the counter are stored.

Other approaches

Counting buckets (time O(n), space O(n))

Count how many elements have each value, capping values above n at n. Walk x from n down to 0 keeping a running total of elements >= x, and return x when the total equals x.

Sort, then scan (time O(n log n), space O(1))

Sort descending. x is special when nums[x - 1] >= x and either x = n or nums[x] < x. Check each x from 1 to n.

Common mistakes

  • Searching x only among the values in nums. The answer need not appear in the array, as [3,5] with answer 2 shows.
  • Leaving out x = 0 or x = n. Both ends of [0, n] are valid candidates, though x = 0 can only work when every element is below 0, which never happens here.
  • Returning the first x with count(x) >= x. That is the h-index style question, but here the count must match exactly, otherwise the answer is -1.

Frequently asked questions

Why is x unique?

count(x) - x strictly decreases as x grows, because count(x) never increases while x increases by one. A strictly decreasing sequence equals zero at most once.

How is this related to H-Index?

H-Index asks for the largest h with at least h values >= h. This problem asks for an x with exactly x values >= x. The same monotone count drives both binary searches.

Is binary search worth it for n up to 100?

For this size any approach is fast. The binary search is still the clean way to use the monotone count, and the counting-bucket version shows how to reach O(n).