Consider the problem of finding an element in an array of size . In which of the following cases can we find the element in time?
A) Array is sorted.
B) Array is sorted and is rotated by . The value of is known.
C) Array is sorted and is rotated by . The value of is unknown.
D) Array is not sorted.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
A nice question! (liked)
Answer is A , B , C .
For A simply use binary search. It's O ( lo g n ) .
For B rotate it back first. Then use binary search. It's O ( 1 ) + O ( lo g n ) = O ( lo g n ) .
For C use binary search to find out value of k first. Then rotate it back. Then use binary search. It's O ( lo g n ) + O ( 1 ) + O ( lo g n ) = O ( lo g n ) .