#K40807. Find Target Range in Sorted Array
Find Target Range in Sorted Array
Find Target Range in Sorted Array
Given a sorted array of integers and a target value, find the starting and ending positions of the target value. Your algorithm must run in O(log n) time complexity. If the target is not in the array, output -1 -1
.
This problem can be solved using a modified binary search to locate the first and last occurrence of the target.
inputFormat
The input is read from stdin
and consists of three parts:
- The first line contains an integer n, the number of elements in the array.
- The second line contains n space-separated integers representing the sorted array.
- The third line contains the target integer.
outputFormat
Output to stdout
two space-separated integers. These represent the starting and ending positions of the target in the array. If the target is not found, output -1 -1
.
6
5 7 7 8 8 10
8
3 4
</p>