447. Count Elements Less Than or Equal to Mid

EasyBinary SearchArrayBinary Search

Given a sorted array nums and an integer mid, return the count of elements in nums that are less than or equal to mid. Solve in O(log n).

Input: A sorted integer array nums and an integer mid.

Output: Count of elements <= mid.

Examples

Example 1
Input: [1,2,3,4,5,6,7], 4
Output: 4
Explanation: Elements <= 4 are {1,2,3,4}. Count = 4.
Example 2
Input: [2,2,2,2,2], 2
Output: 5
Explanation: All 5 elements are <= 2. Count = 5.
Example 3
Input: [1,3,5,7,9], 6
Output: 3
Explanation: Elements <= 6: {1,3,5}. Count = 3.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →