In the first step, we are rejecting one part which is sorted, So Its complexity is O(log n).
if a[i]>a[k]>a[j] => a[k,n] is sorted in decreasing order, So discard this part and search in a[1,k]
else if a[i]<a[k]<a[j] => a[1,k] is sorted in increasing order, So discard this part and search in a[k,n]
else a[i]<a[k]>a[j] => a[k] is result and we are done

in case of 3 elements like 231 k will be 3 but your algo return -1 because low is 0 high is 2 (2+0)/2=1 and 1+1==high (mid+1==high return -1)i don't understand whats problem in this or where i doing some mistake?

Finding out maximum element complexity: O(logn)
Doing two binary searches on different parts of the array: O(log(n1))+O(log(n2))
Total complexity: O(log(n*n1*n2))
You might say that n1=k*n; n2=k1*n;
so Complexity : O(logn(n))

the problem complexity is o(log n)
the problem is divided into 2 parts.
1)finding maximum point
2)applying binary search on each side of maximum index.
to find maximum point:
divide the array into 3 parts using n/3 and 2n/3 as indexes.
if(a[n/3]<a[2n/3])
then maximum present in the a[n/3] to a[n];
else
the maximum present in a[1] to a[2n/3]
repeat the process
the complexity for this is o(log n) because the array size is decrimented each time by 1/3rd.
now apply binary search on both sides of maximum element...

Algo to find the point of inflection, once you get it do binary search in individual parts of the array, time complexity is O(log(n))

def find_pos(arr,n,s,e):
m=(s+e)/2
if not (m-1>=0 and m+1<n): return -1
if arr[m-1]<arr[m] and arr[m]>arr[m+1]: return m
if arr[m-1]>arr[m] and arr[m]>arr[m+1]:
return find_pos(arr,n,s,m)
if arr[m-1]<arr[m] and arr[m]<arr[m+1]:
return find_pos(arr,n,m+1,e)

Algo to find the point of inflection, once you get it do binary search in individual parts of the array, time complexity is O(log(n))

def find_pos(arr,n,s,e):
m=(s+e)/2
if not (m-1>=0 and m+1<n): return -1
if arr[m-1]<arr[m] and arr[m]>arr[m+1]: return m
if arr[m-1]>arr[m] and arr[m]>arr[m+1]:
return find_pos(arr,n,s,m)
if arr[m-1]<arr[m] and arr[m]<arr[m+1]:
return find_pos(arr,n,m+1,e)

It is a Bitonic Sequence.
Let Mid = (start + end)/2, divides the array in two halves.
In this case two cases could happen:
1. The Array is Exactly Increasing/Decreasing in first half and Exactly Decreasing/Increasing in the second half.
2. The Array is Decreasing/Increasing in one half and it is again Bitonic in the other half.

So basically we perform Binary search in the Monotonic sequence and perform Sequential search in the Bitonic sequence.

My idea is same as everyone else:
(1) Find the position of the maximum with modified binary search. Call this the "climax".
(2) Perform binary search from beginning to climax
(3) Perform binary search from climax to end

Each is O(logn) so altogether still O(logn). Note that I am also accommodating for the case when the climax is at the beginning or end of the array.

Hmm sorry I just realized that in my binarySearch, start = 0 doesn't guarantee that we are searching an ascending array, if the maximum element is at the beginning. But that's simple enough to fix by using an additional parameter instead.

-
Working code with O(Log(n)) to find Max element Position
June 16, 2012 | FlagReply

Comment hidden because of low score. Click to expand.

-2

of 2 vote

How about this approach:

1. First find the highest element; If there are elements i,j such that a[i]>a[j], obviously 'i' marked the end of the first list. O(n);
2. Do a binary search on these elements. If it is found, return it. [O(logn)].
3. If it is not there, then search in the decreasing part; For this multiply the numbers 'i+1' till last element with -1 and also the key with -1; O(n)
4. Search using binary search for the key; O(log n);
5. If found, return else terminate with a message saying that the key is not found.
So, overall complexity is O(n) + O(log n ) + O(n) + O(log n ) ;
6. Final complexity, is O( n );

What's Going On

Books

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Resume Review

Most engineers make critical mistakes on their resumes -- we can
fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.