> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/33_search_in_rotated_sorted_array.md).

# 33 Search in Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e.,`0 1 2 4 5 6 7`might become`4 5 6 7 0 1 2`).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

**Complexity:** O(logN) time, O(logN) space

**The Idea:** Use binary search to find index of pivot, which we can identify as the minimum element in the array. In other words, the target value will have a unique position where both its left and right neighbors are the same or larger. Then use binary search to search left or right of pivot.

```cpp
// no need to exit condition because we are guarentee to
// linearly converge to a pivot point (check is made that it exists)
int find_pivot(vector<int> &nums, int low, int high) {
    int mid = low + ((high - low) / 2);

    if (nums.at(mid) > nums.at(mid + 1)) {
        return mid + 1;
    }
    else if (nums.at(low) <= nums.at(mid)) return find_pivot(nums, mid + 1, high);
    else return find_pivot(nums, low, mid - 1);
}

int binary_search(vector<int> &nums, int low, int high, int target) {
    if (high < low) return -1;
    int mid = low + ((high - low) / 2);

    if (nums.at(mid) == target) return mid;
    else if (nums.at(mid) > target) return binary_search(nums, low, mid - 1, target);
    else return binary_search(nums, mid + 1, high, target);
}


int search(vector<int>& nums, int target) {
    if (nums.empty()) return -1;

    int start = 0;
    int end = nums.size() - 1;

    // not rotated
    if (nums.front() <= nums.back()) 
        return binary_search(nums, start, end, target);

    // find pivot point
    int pivot_i = find_pivot(nums, start, end);

    if (nums.at(pivot_i) == target)
        return pivot_i;

    // search right [pivot, end]
    else if (target >= nums.at(pivot_i) && target <= nums.back())
        return binary_search(nums, pivot_i + 1, end, target);

    // search left [start, pivot]
    else return binary_search(nums, start, pivot_i - 1, target);
}
```

**Testing**

```cpp
int main()
{
    // t1
    vector<int> nums1 = { 1 };
    assert(search(nums1, 1) == 0);
    assert(search(nums1, -1) == -1);

    // t2
    vector<int> nums2 = { 4, 5, 6, 7, 0, 1, 2 };
    for (int i = 0; i < nums2.size(); i++) 
        assert(search(nums2, nums2.at(i)) == i);
    assert(search(nums2, 10) == -1);

    // t3
    vector<int> nums3 = { 4, 5, 1, 2, 3 };
    for (int i = 0; i < nums3.size(); i++)
        assert(search(nums3, nums3.at(i)) == i);
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/33_search_in_rotated_sorted_array.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
