Skip to content
PostAlgorithm / Basic Algorithm

Binary Search

2024-09-14
Back to Blog

Binary Search ​

CompareRepresent
≥lowerbound
>≥(x+1)
<≥(x)−1
≤>x−1
cpp
// >=
int index = lower_bound(X.begin(),X.end(),x)-X.begin();
// >
int index = lower_bound(X.begin(),X.end(),x+1)-X.begin();
// <
int index = lower_bound(X.begin(),X.end(),x)-1-X.begin();
//<=
int index = lower_bound(X.begin(),X.end()+1,x+1)-1-X.begin();