code:https://ide.codingblocks.com/s/111905
what is the error in the code?
Find upper and lower bound
it is not passing the test case.Giving wrong answer.
hey @dvitiarora, just a small change that I have indicated as comments. Please check it here https://ide.codingblocks.com/s/112041
My base case 0 is not passing rest all are passing kindly help me as well.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll firstOccurance(ll arr[],ll n,ll m){
ll s=0,e=n;
ll ans=-1;
while(s<=e){
ll mid=(s+e)/2;
if(arr[mid]==m){
ans=mid;
e=mid-1;
}
else if(arr[mid]>=m)
e=mid-1;
else
s=mid+1;
}
return ans;
}
ll lastOccurance(ll arr[],ll n,ll m){
ll s=0,e=n;
ll ans=-1;
while(s<=e){
ll mid=(s+e)/2;
if(arr[mid]==m){
ans=mid;
s=mid+1;
}
else if(arr[mid]>=m)
e=mid-1;
else
s=mid+1;
}
return ans;
}
int main(){
ll n,m;
cin>>n;
ll arr[n];
for(ll i=0;i<n;i++)
cin>>arr[i];
int q;
cin>>q;
while(q–){
cin>>m;
cout<<firstOccurance(arr,n,m)<<" "<<lastOccurance(arr,n,m)<<endl;
}
return 0;
}