← 返回 bytedance 的题目列表Find the First Element Greater than K
类型:online_judge
bytedance
Given a sorted integer array arr and an integer k, perform a binary search to find the first element in the array that is greater than k, and return its index. If such an element does not exist, return -1.
Input Description:
A space-separated list of integers arr, the array is sorted in ascending order.
An integer k.
Output Description:
The index of the found element, or -1 if it doesn't exist.
Example:
Input:
arr = [1, 3, 5, 7, 9]
k = 4
Output:
2
Note:
The length of arr is n, and it satisfies 1 <= n <= 10^5.
arr[i] is a 32-bit integer.
The solution should be efficient with a worst-case time complexity of O(log n).
Example
Input
1 3 5 7 9
4