alan4ik's blog

By alan4ik, history, 4 years ago, In English

I am a big fan of Ternary Search.

But the fact that we can use it only in strictly increasing and strictly decreasing functions upsets me.

I am asking how to deal with functions which can have equal values (i.e. $$$f(i) <= f(i + 1)$$$ $$$[i < j]$$$, $$$f(i) >= f(i + 1)$$$ $$$[i >= j]$$$).

For example with this function $$$f(x) =$$$ {$$$1, 2, 1, 1, 1, 1, 1, 1, 1, 1$$$}.

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

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

You can't, really, without any additional information. What if I gave you an array with $$$999\,999$$$ ones and $$$1$$$ two, and wanted you to find the maximum? Even if you queried $$$500\,000$$$ elements of my array, you'd have at most $$$50\%$$$ success probability.