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:
1 <= nums.length <= 100
0 <= nums[i] <= 1000
This problem asks us to find a number x
such that there are exactly x
numbers in the input array nums
that are greater than or equal to x
. If such an x
exists, we return it; otherwise, we return -1.
Approach 1: Brute Force
This approach iterates through all possible values of x
from 1 to the length of the array. For each x
, it counts the number of elements in nums
that are greater than or equal to x
. If this count equals x
, we've found our special number and return it. If the loop completes without finding such a number, we return -1.
Time Complexity: O(n^2), due to the nested loops (outer loop iterates through possible x values, inner loop counts elements). Space Complexity: O(1), constant extra space.
Approach 2: Sorting + Binary Search
This approach improves efficiency by first sorting the input array. After sorting, for each potential x
, we can use binary search to efficiently find the number of elements greater than or equal to x
.
The sorting step takes O(n log n) time. The subsequent loop and binary search operations take O(n log n) time in the worst case (if we have to perform a binary search for each x).
Time Complexity: O(n log n), dominated by the sorting step and the binary search within the loop. Space Complexity: O(log n) for the space used by the sorting algorithm (depending on the specific sorting implementation). In-place sorting algorithms would have a space complexity of O(1).
Code Examples (Python):
Approach 1 (Brute Force):
class Solution:
def specialArray(self, nums: List[int]) -> int:
for x in range(1, len(nums) + 1):
count = sum(1 for num in nums if num >= x)
if count == x:
return x
return -1
Approach 2 (Sorting + Binary Search):
import bisect
class Solution:
def specialArray(self, nums: List[int]) -> int:
nums.sort()
n = len(nums)
for x in range(1, n + 1):
count = n - bisect.bisect_left(nums, x) #Efficient count using binary search
if count == x:
return x
return -1
Other Languages: The algorithms can be implemented similarly in other languages like Java, C++, JavaScript, etc. The core logic remains the same; the only differences would be in syntax and potentially the specific binary search implementation used. For example, Java's Arrays.binarySearch()
could be used in the second approach. The code for other languages was provided in the original prompt and is omitted here for brevity.