#C5810. Find Target Range in Sorted Array

    ID: 49501 Type: Default 1000ms 256MiB

Find Target Range in Sorted Array

Find Target Range in Sorted Array

Given a sorted array of integers \(nums\) in non-decreasing order and a target integer \(target\), your task is to find the starting and ending indices of \(target\) in the array. If \(target\) does not exist in the array, output \(-1 -1\). Indices are 0-indexed.

You are required to use binary search to achieve a time complexity of \(O(\log n)\) for each query. In other words, you need to implement two binary searches: one to find the left-most (first) occurrence and one to find the right-most (last) occurrence of \(target\) in \(nums\).

For example:

Input: 6
       5 7 7 8 8 10
       8
Output: 3 4

If the target is not found, print -1 -1.

inputFormat

The first line contains an integer \(n\), denoting the number of elements in the array \(nums\). The second line contains \(n\) space-separated integers representing the elements of \(nums\) (sorted in non-decreasing order). The third line contains a single integer \(target\), whose range in the array you need to determine.

Input Format:

 n
 nums[0] nums[1] ... nums[n-1]
 target

outputFormat

Print two space-separated integers representing the starting and ending indices of \(target\) in \(nums\). If \(target\) is not present, print -1 -1.

Output Format:

 start_index end_index
## sample
6
5 7 7 8 8 10
8
3 4

</p>