Easist C++ Solution (BS)


#1

int findStarting(vector A, int B){
int start =0;
int end = A.size()-1;
int ans=-1;
while(start<=end){
int mid = start + (end-start)/2;
if(A[mid]== B){
ans = mid;
end = mid-1;
}else if(A[mid]>B){
end = mid-1;
}else{
start = mid+1;
}
}
return ans;
}
int findEnding(vector A, int B){
int start =0;
int end = A.size()-1;
int ans=-1;
while(start<=end){
int mid = start + (end-start)/2;
if(A[mid]== B){
ans = mid;
start = mid+1;
}else if(A[mid]>B){
end = mid-1;
}else{
start = mid+1;
}
}
return ans;
}

vector Solution::searchRange(const vector &A, int B) {
int S = findStarting(A, B);
int E = findEnding(A, B);
vector ans ={S, E};
return ans;
}