# 1608. Special Array With X Elements Greater Than or Equal X ###### tags: `Leetcode` `Binary Search` `Easy` Link: https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/ ## 思路 $O(NlogN)$ $O(1)$ ## Code ```java= class Solution { public int specialArray(int[] nums) { int start = 0; int max = 0; for(int num:nums) max = Math.max(max, num); int end = Math.min(max, nums.length); while(start<end){ int mid = start+(end-start)/2; int numOfGreaterorEqual = count(nums, mid); if(numOfGreaterorEqual > mid){ start = mid+1; } else end = mid; } return count(nums, start)==start?start:-1; } private int count(int[] nums, int x){ int count = 0; for(int num:nums) if(num>=x) count++; return count; } } ```