saketkumarsingh's blog

By saketkumarsingh, history, 15 months ago, In English

I was asked this question in the interview round of a company: Given an array of integers of size atleast 3, such that a[0]>=a[1].... And a[n-2]<=a[n-1] such that it gives a v shape (i.e. first it’s decreasing then its increasing) find the minimum element in the array.

The interviewer asked to implement in O(log n) , if all the elements would have been distinct then I coded the approach using binary search but could not come up with a log n solution in case of duplicates. I was asked to code it using recursion also. Can someone help me out?

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

| Write comment?
»
15 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by saketkumarsingh (previous revision, new revision, compare).

»
15 months ago, # |
  Vote: I like it +95 Vote: I do not like it

If there can be duplicates, it's possible for the array to look like 222222122222222, where it's obviously impossible to find the 1 faster than $$$O(n)$$$.

»
15 months ago, # |
  Vote: I like it +19 Vote: I do not like it

Well, applying binary search at its core requires the programmer to decide how to transition your search space after every call. But in the case of duplicates, it's not actually possible to make a decision. Let's see how ;)

Case

So maybe he/she wanted to know your intuitive proof that this problem cannot be solved in less than O(N). There's a similar question too on Leetcode with these two variants.

Search In A Rotated Sorted Array I(Distinct)

Search In A Rotated Sorted Array II(Duplicates)