2
$\begingroup$

Suppose we are given A, an array of size n, comprised of an increasing sequence of numbers followed immediately by a decreasing one. What is worst case time complexity of optimal algorithm to determine if a given number x is in the array?

The explanation given on the this site is confusing to me. How we can apply binary search on unsorted array?

They have provided following explanation => This is an application of Binary search, which has time complexity $Θ(log n)$ in worst case.

asked Jan 11, 2018 at 22:51
$\endgroup$
1

2 Answers 2

3
$\begingroup$

We can find the break point (where the type of sequence changes) in theta(log n). Then we can search individually on both sides of the break point (each of which is a sorted sequence). So overall theta(log n) only.

answered Jan 12, 2018 at 5:16
$\endgroup$
2
$\begingroup$

First use binary search to find the index, where the ordering changes from increasing to decreasing. Now you can use binary search on both partitions of the array.

answered Jan 11, 2018 at 23:00
$\endgroup$
1
  • $\begingroup$ You mean to say break point increasing to decreasing. $\endgroup$ Commented Jan 11, 2018 at 23:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.