C++ solution using lower_bound and upper_bound stl

vector Solution::searchRange(const vector &v, int t) {
if(!binary_search(v.begin(),v.end(),t))
return {-1,-1};
int l=(lower_bound(v.begin(),v.end(),t)-v.begin());
int r=(upper_bound(v.begin(),v.end(),t)-v.begin())-1;
return {l,r};
}

Same idea here :slight_smile: No need for additional binary search to exclude cases when the number is not present:

vector<int> Solution::searchRange(const vector<int> &A, int B)
{
    auto itl = std::lower_bound(A.begin(), A.end(), B);
    auto itu = std::upper_bound(A.begin(), A.end(), B);
    
    if (itl == itu) return {-1, -1};
    
    return {itl - A.begin(), itu - A.begin() - 1};
}
Click here to start solving coding interview questions