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

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

Leetcode Solutions

Sorting and Binary Search

  1. Sort the array nums in non-decreasing order.
  2. Initialize left to 0 and right to the length of nums.
  3. While left is less than or equal to right: a. Calculate mid as the average of left and right. b. Use binary search to find the first index where the value is greater than or equal to mid. c. Calculate the number of elements greater than or equal to mid. d. If the count is equal to mid, return mid as the answer. e. If the count is less than mid, adjust right to mid - 1. f. If the count is greater than mid, adjust left to mid + 1.
  4. If no such x is found, return -1.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...