TLE problem (its for asking any solution but im not getting)

#include
int bsearch(int [],int,int);
using namespace std;

int main()
{
int n,num,index;

cin>>n;
int a[n];

for(int i=0;i<n;i++){
    cin>>a[i];
    
}

cin>>num;
index=bsearch(a,n,num);
if(index==-1){
   
}
else{
    cout<<index;
}

return 0;

}
int bsearch(int a[],int n,int ele){
int beg=0;
int last=n-1;
while(beg<=last){
int mid=(beg+last)/2;
if(ele==a[mid]){
return mid;

    }
    else if(ele>a[mid]){
        beg=mid+1;
    }
    else {last =n-1;}
}
return -1;

}

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.