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.
I have this implementation that I found really helpful:
you just have to implement the function $$$f$$$, that depends on the problem.
Yeah this is actual implementation of Terneray Search and how it should be used but the way I have implemented you can see it is more like Binary Search even complexity wise it would be O(log2n)
I just always found that implementation easier
But Yeah This is exactly how Ternary Search should be used Thanks for the Input man
Why eps=5, in case of integers?
I think it should be 3, because when r-l<=3, then we are not able to differentiate between l,m1,m2 and r hence break out of loop.
time complexity?
Yeah since difference is the derivative, it’s a smart idea