Created
March 25, 2019 04:30
-
-
Save saikpr/a4b8a8a76be2695677880bc5657a09c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This version maintains these invariants: | |
good < bad | |
!isBad(good) | |
isBad(bad) | |
i.e. it keeps pushing good and bad together until they are adjacent to each other. | |
["Good","Good","Good","Good","Good","Bad","Bad","Bad","Bad","Bad"] | |
to find the last "Good" | |
int findBadRev(unsigned int good, unsigned int bad) | |
{ | |
while (good < bad-1) { // note the -1 here | |
unsigned int mid = (good + bad)/2; | |
if (isBad(mid)) { | |
bad = mid; | |
} else { | |
good = mid; | |
} | |
} | |
return bad; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment