Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
class Solution {
public:vector<int> searchRange(int A[], int n, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int vist[2] ={0,0};
int count1=0, count2=n-1;
vector<int> result(2,-1);
while(count1 < count2 && (vist[0]==0 || vist[1]==0))
{
if(A[count1] == target)
{
vist[0] = 1;
result[0] = count1;
}
if(! vist[0])
{
count1++;
}
if(A[count2] == target)
{
vist[1] = 1;
result[1] = count2;
}
if( ! vist[1] )
{
count2--;
}
}
if(count1 == count2 && A[count1] == target)
{
result[0] = count1;
result[1] = count1;
}
return result;
}
};