pop3's blog

By pop3, history, 4 years ago, In English

Suppose for a function f(x) , for different values of x , f(x) is a bool value yes(y) or no(n) as:-

$$$nnnnnyyyyynnnnn$$$

Also suppose the check function in bsearch returns (-1) if its in the left 'n' block , (0) if its a 'y', and (1) if its in the right 'n' block.

So I need to find the largest value of x for which f(x) is yes(y). Is it doable by binary search?

(Usually we have f(x) varying as $$$yyyyyynnnnnn$$$ or $$$nnnnnnyyyyyy$$$ and in that we can easily use bsearch.)

  • Vote: I like it
  • +2
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +9 Vote: I do not like it

Yes, you can use binary search. The easiest way to "visualize it" is this: Treat the left 'n' block as a 'y'. Then your string becomes: $$$yyyyyyyyyyynnnnnnnnn$$$ and you can apply standard binary search.