Getting run time error while running the code

while running the code i am getting runtime error for few test case and correct for another cases ,while running again i am getting correct for other test cases and run time error test cases which were correct first bottom line is i am getting different answers for each test cases every time

here is my code

#include
using namespace std;
int BinarySearch(int a[],int n,int key){
int s=0,e=n-1;
int mid;
while(s<=e){
mid=(s+e)/2;
if(a[mid]==key)
return mid;
else if(a[mid]>key)
e=mid-1;
else
s=mid+1;
}
return -1;
}
int main(){
int n,key,a[n];
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
cin>>key;
if(BinarySearch(a,n,key))
cout<<BinarySearch(a,n,key);
else
cout<<BinarySearch(a,n,key);

}

@Himanshu0123 hey himanshu your array size is a garbage value how can you declare your array before taking input n declare after taking input n it will resolve your problem if it will not resolve then do tag…