Hello.

I gave written test at Adobe, Bangalore. There was a question to optimize Binary search. Below is the normal code for Binary search. How can we optimize it so that there is only one comparision made within loop inside bsearch function.

Thanks in advance for your suggestions !!

Code:#include <iostream> using namespace std; bool bsearch(int arr[], int limit, int num){ int low = 0, high = limit; int mid = (low+high)/2; while(low<=high && arr[mid] != num){ if(num > arr[mid]) low = mid + 1; else high = mid -1; mid = (low + high)/2; } if(arr[mid] == num){ cout<<"the number is found at position"<< mid; } else{ cout<<"\nthe number is not found"; } } int main(){ int a[] = {2,4,6,8,10,12}; bool status = bsearch(a,5,2); }