What's wrong in code


#1
int Solution::search(const vector<int> &A, int B) {
vector<pair<long int,long int>> b;
long int i,j;
for(i=0;i<A.size();i++)
{
    b.push_back(make_pair(A[i],i));
}
sort(b.begin(),b.end());
long int l,h,m;
l=0,h=A.size()-1;
while(l<=h)
{
    m=(l+h)/2;
    if(b[m].first==B)
    {
        return b[m].second;
    }
    if(b[m].second<B)
    {
        l=m+1;
    }
    else
    {
        h=m-1;
    }
}
return -1;
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

}


#2

It fails to pass some test cases.
Please help to figure out what’s wrong…


#3

U should not apply this approach as sort would take O(nlogn) time and this problem could be solved in logn time