Special Array With X Elements Greater Than or Equal X (LeetCode 1608): Solution and Explanation
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
- Set
lo = 0andhi = n. - While
lo <= hi, takex = (lo + hi) / 2and count the elements>= xwith one pass. - If the count equals
x, returnx. - If the count is larger than
x, every smallerxhas an even larger count, so setlo = x + 1. - Otherwise set
hi = x - 1. - If the range empties, return -1.
Step-by-step example
nums = [3,5]
- lo = 0, hi = 2.
- x = 1: both 3 and 5 are >= 1, count 2 > 1, so lo = 2.
- 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 -11class Solution {
2 public int specialArray(int[] nums) {
3 int lo = 0, hi = nums.length;
4 while (lo <= hi) {
5 int x = lo + (hi - lo) / 2;
6 int count = 0;
7 for (int v : nums) {
8 if (v >= x) {
9 count++;
10 }
11 }
12 if (count == x) {
13 return x;
14 }
15 if (count > x) {
16 lo = x + 1;
17 } else {
18 hi = x - 1;
19 }
20 }
21 return -1;
22 }
23}1class Solution {
2public:
3 int specialArray(vector<int>& nums) {
4 int lo = 0, hi = nums.size();
5 while (lo <= hi) {
6 int x = lo + (hi - lo) / 2;
7 int count = 0;
8 for (int v : nums) {
9 if (v >= x) {
10 count++;
11 }
12 }
13 if (count == x) {
14 return x;
15 }
16 if (count > x) {
17 lo = x + 1;
18 } else {
19 hi = x - 1;
20 }
21 }
22 return -1;
23 }
24};Time and space complexity
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.
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).