Binary Search

Here is my way of implementing the Binary search
Necessary Conditons:

  1. Answer must be in the sample space, if not known 0 to Long long int may be preferred
  2. Yes / No should be followed…
  3. Monotonicity.

The pseudo code

Binary Search Approach

int lo,hi;  

while(lo<=hi)  
{  
        int mid=lo+(hi-lo)/2;  
	check the condition;  
	if()  
	ans=mid;hi=mid-1; // The condition which contains answer, in that make ans=mid  
	else  
	lo=mid+1;  
}  
cout<<ans;