0

An integer sorted array rotated to left by unknown no of times, write an efficient algorithm to search for an element.

Example: 4 5 6 7 8 9 1 2 3 4

I am thinking every time I find a mid in Binary Search I compare the element with extreme end element and take a decision on which half to pick to repeat the process. Is it wrong? Or is there any efficient algo?

orlp
  • 112,504
  • 36
  • 218
  • 315
Vijay Muvva
  • 1,063
  • 1
  • 17
  • 31

1 Answers1

0

Your example array contains duplicates. When there are duplicates there is no efficient algorithm - you must always do O(n) work in the worst case.

To prove this, consider arrays of this form:

000000000000000010000000

It is a rotation of a sorted array, but in the worst cast you must iterate over every element to see where the 1 is.

orlp
  • 112,504
  • 36
  • 218
  • 315