The Indian Engineer

Problem 1608 Special Array With X Elements Greater Than or Equal X

Posted on 6 mins

Array Binary-Search Sorting

Problem Statement

Link - Problem 1608

Question

You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x.

Notice that x does not have to be an element in nums.

Return x if the array is special, otherwise, return -1. It can be proven that if nums is special, the value for x is unique.

Example 1:

Input: nums = [3,5]
Output: 2
Explanation: There are 2 values (3 and 5) that are greater than or equal to 2.

Example 2:

Input: nums = [0,0]
Output: -1
Explanation: No numbers fit the criteria for x.
If x = 0, there should be 0 numbers >= x, but there are 2.
If x = 1, there should be 1 number >= x, but there are 0.
If x = 2, there should be 2 numbers >= x, but there are 0.
x cannot be greater since there are only 2 numbers in nums.

Example 3:

Input: nums = [0,4,3,0,4]
Output: 3
Explanation: There are 3 values that are greater than or equal to 3.

Constraints:

Solution

class Solution {
public:
    int specialArray(vector<int>& nums) {
        int bucket[1001] = {0};
        for (int num : nums) {
            bucket[num]++;
        }
        int total = nums.size();
        for (int i = 0; i < 1001; i++) {
            if (i == total) {
                return i;
            }
            total -= bucket[i];
        }
        return -1;
    }
};

Complexity Analysis

| Algorithm       | Time Complexity | Space Complexity |
| --------------- | --------------- | ---------------- |
| Bucket Counting | O(n)            | O(1)             |

Explanation

Intial Thoughts

To approach this problem, we should first understand what a special array is. It’s an array where there exists a number x such that there are exactly x numbers in the array that are greater than or equal to x. We can think of it as finding a balance point where the number of elements greater than or equal to x is equal to x itself. We should consider all possible values of x and check if the condition is met. We can use a systematic approach to iterate through all possible values of x and count the numbers greater than or equal to x.

Intuitive Analysis

Intuitively, we can solve this problem by iterating through all possible values of x and checking if the condition is met. We can imagine a bucket system where each bucket represents a value of x, and we count the numbers greater than or equal to x. We can start from the smallest possible value of x, which is 0, and increment it until we find a match or reach the maximum possible value. We can also consider the fact that x does not have to be an element in the array, so we need to check all possible values, not just the ones present in the array. By systematically checking all possible values of x, we can find the special number if it exists.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Easy difficulty.

Hints

Count the number of elements greater than or equal to x for each x in the range [0, nums.length].

If for any x, the condition satisfies, return that x. Otherwise, there is no answer.