Ternay Search is a very cool upgrade/modification on Binary Search logic There are enough resources available on this topic my favorite resource is :-
This blog will include a different implementation of the approach which I found very cool
int l=1,r=mx;
while(r>=l){
int mid = (r+l)/2;
int m1 = check(mid);
int m2 = check(mid+1);
if(m1>=m2){
ans = updateAns(and,m1);
r=mid-1;
}
else{
l=mid+1;
ans = updateAns(and,m2);
}
}
It would make more sense if you study the concept and then solve these 2 problems these are fairly recent problems as well
Would love any input to improve this blog.