My code is failing 3 test cases for very large inputs(time limit) …I think the problem is with auto it operator…
#include <bits/stdc++.h>
int main()
{
long long int n,k;
cin>>n;
vector<int >v;
while(n--)
{
cin>>k;
v.push_back(k);
}
sort(v.begin(),v.end());
long long int query,x;
cin>>query;
while(query--)
{
cin>>x;
auto it = find(v.begin(),v.end(),x);
if(it!=v.end())
{
cout<<"Yes "<<it-v.begin()+1<<endl;
}
else
{
auto it1=lower_bound(v.begin(),v.end(),x);
cout<<"No "<<it1-v.begin()+1<<endl;
}
}
return 0;
}
Please format your code - the forum software has mangled it and it won’t compile!
Edit:
while(query--)
{
cin>>x;
auto it = find(v.begin(),v.end(),x);
This loop is executed O(Q) times and std::find is linear-time, so each call takes O(N), so this is an O(N \times Q) algorithm - too slow to run in ~2 secs with N=10^5 and Q=10^5.
This is one of the weirdest in built function I feel.
I mean I end up using my own binary search function everytime because I have certain requirements and I don’t use standard binary search everytime.
I know in-built functions are useful to save a lot of time so I wanted to know if there’s a way to like modify the function to our need?
Btw to use the array value
int x=a[lower_bound(a.begin(),a.end(),target)-a.begin()]