Algorithms and Data Structures · Middle · Question 16 of 75

How do you find a value in a sorted array that was rotated, like [4, 5, 6, 1, 2, 3], in O(log n)?

Explanation

Compare arr[lo] with arr[mid] to see which half is in order, then check whether the target falls into that half's range. Sorting first costs O(n log n), linear search O(n).

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 16

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz