Could you please help me how to avoid TLE in the below binary search problem :
Question : https://www.spoj.com/problems/BSEARCH1/ My solution works fine but the problem in editor but when submitting it online it shows TLE. Could you please help me out of this problem. My solution: // Program to find an element of a sorted array using binary search. #include<bits/stdc++.h> using namespace std; int binarysearch(int arr[], int n,int x){ int left=0,right=n-1,mid; while(left<=right) { mid=(left+right)/2; if(arr[mid]==x) return mid; else if(x<arr[mid]) right=mid-1; else left=mid+1; if(left>right) return -1; } } int main(){ int n,q,pos; scanf("%d %d",&n,&q); int arr[n]; for(int i=0;i<n;i++){ cin>>arr[i]; } while(q>0){ q--; int x; cin>>x; pos=binarysearch(arr,n,x); cout<<pos<<"\n"; } }