Special Array With X Elements Greater Than or Equal X

EasyBinary Searchbinary search

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an array nums of non-negative integers. The array is *special* if there is an integer x such that exactly x elements of nums are greater than or equal to x. x does not have to be an element of nums. If such an x exists it is unique; return it. Otherwise return -1.

Examples

Input: nums = [3,5]
Output: 2
Exactly 2 values (3 and 5) are >= 2.
Input: nums = [0,0]
Output: -1
x = 0 fails (2 values are >= 0), x = 1 fails (0 values), x = 2 fails (0 values).
Input: nums = [0,4,3,0,4]
Output: 3
Exactly 3 values (4, 3, 4) are >= 3.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search problems