Binary Search
Here is my way of implementing the Binary search
Necessary Conditons:
- Answer must be in the sample space, if not known 0 to Long long int may be preferred
- Yes / No should be followed…
- 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;