C++ Simple and efficient code in few lines

programming
google
Tags: #<Tag:0x00007f2426785158> #<Tag:0x00007f2426785018>

#1
vector<int> Solution::searchRange(const vector<int> &A, int B) {
vector<int> v;
if(!binary_search(A.begin(), A.end(), B)) return {-1, -1};
int l_idx=lower_bound(A.begin(), A.end(), B)-A.begin();
if(l_idx==0 && A[l_idx]!=B) l_idx=-1;
v.push_back(l_idx);
v.push_back(upper_bound(A.begin(), A.end(), B)-A.begin()-1);
return v;
}

#2

the second if statement is useless, since you are already checking for the presence of the element in the first one!