Skip to content

Instantly share code, notes, and snippets.

@saikpr
Created March 25, 2019 04:30
Show Gist options
  • Save saikpr/a4b8a8a76be2695677880bc5657a09c7 to your computer and use it in GitHub Desktop.
Save saikpr/a4b8a8a76be2695677880bc5657a09c7 to your computer and use it in GitHub Desktop.
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