Time limit exceed

could you tell me the efficient way to solve this problem as the logic that i am applying is resulting in exceeding of time limit.
my code is-
#include
using namespace std;
int first(int a[],int n,int key)
{
int s=0,e=n-1;
int ans=-1;
while(s<=e)
{
int mid=(s+e)/2;
if(a[mid]==key)
{
ans=mid;
e=mid-1;
}
else if(key<a[mid])
{
e=mid-1;
}
else
{
s=mid+1;
}
}
return ans;
}
int last(int a[],int n,int key)
{
int s=0,e=n-1;
int ans=-1;
while(s<=e)
{
int mid=(s+e)/2;
if(a[mid]==key)
{
ans=mid;
s=mid+1;
}
else if(key<a[mid])
{
e=mid-1;
}
else
{
s=mid+1;
}
}
return ans;
}
int main() {
int n;
cin>>n;
int i,arr[n];
for(i=0;i,n;i++)
{
cin>>arr[i];
}
int t;
cin>>t;
while(t>0)
{
int num;
cin>>num;
cout<<first(arr,n,num)<<" "<<last(arr,n,num)<<endl;
t–;
}

return 0;

}

plz send your code by saving on ide only.

Here is my code- https://ide.codingblocks.com/s/136506

I have edited your code, try to submit now…