on compiling the code I am getting segmentation fault. Kindly help me in rectifying this error.
Compilation error
Hey, you need not write the implementation of lower and upper bound you can simply use STL to solve this question
Tried the following methodology but it’s still showing segmentation fault.
Code:
#include
#include<bits/stdc++.h>
using namespace std;
void lowerupperbnd(vector arr,vector queries)
{
vector < pair<int , int > > ans;
for(int i=0;i<queries.size();i++)
{
vector<int>::iterator l,u;
l= lower_bound(arr.begin(),arr.end(),queries[i]);
u=upper_bound(arr.begin(),arr.end(),queries[i]);
ans.push_back(make_pair((l-arr.begin()),(u-arr.begin())));
}
for(int i=0;i<ans.size();i++)
{
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
}
int main() {
int n;
vector< int > arr;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int k;
cin>>k;
vector<int> queries;
for(int j=0;j<k;j++)
{
cin>>queries[j];
}
lowerupperbnd(arr,queries);
return 0;
}
it is also possible that the given number is not present in the array pelase use binary_search stl before lower_bound and upper_bound
Yeah right. Will try. Thanks
Your code has too many little mistakes, please try to write a more clean code, if you need some help please refer to this
Ohhk. Thank you so much!
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.