Given a sorted array of integers, find the starting and ending position of a Given target value.
Your algorithm ' s runtime complexity must is in the order of O(log n).
If the target is not a found in the array, return [-1, -1]
.
for example,
given [5, 7, 7, 8, 8, ten]
and target value 8,
return [3, 4]
.
#include <iostream> #include <vector>using namespace std;vector<int> searchrange (int a[], int n, int Target) {int first = 0;int last = N-1;vector<int>result (2,-1); when (first<=last) {int mid = (first + last) /2;if (A[mid] = = target) {result[0] = mid;result[1] = mid;while (result[0]-1 >= first&&a[result[0]-1] = = Targe T)//When a repeat bit is in the range with the new--result[0];while (result[1]+1 <= last&&a[result[1]+1] = = target) ++result[1];return Result;} else if (A[mid] < target) First = mid + 1;elselast = mid-1;} return result;}
Search for a Range