← 返回 citadel 的题目列表Find All Elements Equal to K in a Sorted Array Using Only lower_bound
类型:online_judge
Problem
Given a non-decreasing sorted array nums and a target value k, return the index interval [L, R] containing all elements equal to k. If k does not exist, return [-1, -1].
There is an additional restriction: you may only use one binary-search API:
lower_bound(x): returns the first index i such that nums[i] >= x; returns n if no such index exists.
You are not allowed to implement upper_bound, nor are you allowed to write another variant of binary search.
Basic Version
Assume all elements in nums are integers. Use only lower_bound to find the interval of all elements equal to k.
Follow-up Discussion
If the element type is not integer, such as float, string, or a custom comparable object, and the type has no notion of a successor value, is it always possible to find the right boundary in O(log n) using only lower_bound? Explain why or why not.
Input Format
n k
nums[0] nums[1] ... nums[n-1]
Output Format
L R
If the target does not exist, output:
-1 -1
Constraints
0 <= n <= 2 * 10^5
nums is sorted in non-decreasing order
In the basic version, nums[i] and k are integers
Indices are 0-based
Example
Input:
7 2
1 2 2 2 3 4 5
Output:
1 3
Example
Input
7 2
1 2 2 2 3 4 5
Output
1 3