|
| 1 | +--- |
| 2 | +id: search-in-rotated-sorted-array |
| 3 | +title: Search in Rotated Sorted Array (LeetCode) |
| 4 | +sidebar_label: 0033-SearchInRotatedSortedArray |
| 5 | +description: Search for a target element in a rotated sorted array with distinct values using an algorithm with O(log n) runtime complexity. |
| 6 | +--- |
| 7 | + |
| 8 | +## Problem Description |
| 9 | + |
| 10 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 11 | +| :---------------- | :------------ | :--------------- | |
| 12 | +| [Merge Two Sorted Lists](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/search-in-rotated-sorted-array/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +There is an integer array nums sorted in ascending order (with distinct values). |
| 17 | + |
| 18 | +Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). |
| 19 | + |
| 20 | +Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. |
| 21 | + |
| 22 | +You must write an algorithm with $O(log n)$ runtime complexity. |
| 23 | + |
| 24 | +### Examples |
| 25 | + |
| 26 | +#### Example 1 |
| 27 | + |
| 28 | +- **Input:** nums = [4,5,6,7,0,1,2], target = 0 |
| 29 | +- **Output:** 4 |
| 30 | +- **Explanation:** 0 is located at index 4 in the rotated sorted array [4,5,6,7,0,1,2]. |
| 31 | + |
| 32 | +#### Example 2 |
| 33 | + |
| 34 | +- **Input:** nums = [4,5,6,7,0,1,2], target = 3 |
| 35 | +- **Output:** -1 |
| 36 | +- **Explanation:** 3 is not in nums, so return -1. |
| 37 | + |
| 38 | +#### Example 3 |
| 39 | + |
| 40 | +- **Input:** nums = [1], target = 0 |
| 41 | +- **Output:** -1 |
| 42 | +- **Explanation:** 0 is not in nums, so return -1. |
| 43 | + |
| 44 | +### Constraints |
| 45 | + |
| 46 | +- $v1 <= nums.length <= 5000$ |
| 47 | +- $-10^4 <= nums[i] <= 10^4$ |
| 48 | +- All values of $nums$ are unique. |
| 49 | +- nums is an ascending array that is possibly rotated. |
| 50 | +- $-10^4 <= target <= 10^4$ |
| 51 | + |
| 52 | +### Approach |
| 53 | + |
| 54 | +To search for a target element in a rotated sorted array with distinct values with $O(log n)$ runtime complexity, we can use the binary search algorithm. |
| 55 | + |
| 56 | +1. **Find the Pivot Point:** |
| 57 | + - Perform binary search to find the pivot element, which is the smallest element in the array. This element divides the array into two sorted subarrays. |
| 58 | + |
| 59 | +2. **Perform Binary Search:** |
| 60 | + - Based on the pivot element, determine which half of the array the target might be in. |
| 61 | + - Perform binary search in the appropriate half to find the target element. |
| 62 | + |
| 63 | +### Solution Code |
| 64 | + |
| 65 | +#### Python |
| 66 | + |
| 67 | +```py |
| 68 | +class Solution: |
| 69 | + def search(self, nums: List[int], target: int) -> int: |
| 70 | + left, right = 0, len(nums) - 1 |
| 71 | + while left <= right: |
| 72 | + mid = (left + right) // 2 |
| 73 | + if nums[mid] == target: |
| 74 | + return mid |
| 75 | + elif nums[left] <= nums[mid]: |
| 76 | + if nums[left] <= target < nums[mid]: |
| 77 | + right = mid - 1 |
| 78 | + else: |
| 79 | + left = mid + 1 |
| 80 | + else: |
| 81 | + if nums[mid] < target <= nums[right]: |
| 82 | + left = mid + 1 |
| 83 | + else: |
| 84 | + right = mid - 1 |
| 85 | + return -1 |
| 86 | +``` |
| 87 | + |
| 88 | +#### Java |
| 89 | + |
| 90 | +```java |
| 91 | +class Solution { |
| 92 | + public int search(int[] nums, int target) { |
| 93 | + int left = 0, right = nums.length - 1; |
| 94 | + while (left <= right) { |
| 95 | + int mid = (left + right) / 2; |
| 96 | + if (nums[mid] == target) |
| 97 | + return mid; |
| 98 | + else if (nums[left] <= nums[mid]) { |
| 99 | + if (nums[left] <= target && target < nums[mid]) |
| 100 | + right = mid - 1; |
| 101 | + else |
| 102 | + left = mid + 1; |
| 103 | + } else { |
| 104 | + if (nums[mid] < target && target <= nums[right]) |
| 105 | + left = mid + 1; |
| 106 | + else |
| 107 | + right = mid - 1; |
| 108 | + } |
| 109 | + } |
| 110 | + return -1; |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +#### C++ |
| 116 | + |
| 117 | +```cpp |
| 118 | +class Solution { |
| 119 | +public: |
| 120 | + int search(vector<int>& nums, int target) { |
| 121 | + int left = 0, right = nums.size() - 1; |
| 122 | + while (left <= right) { |
| 123 | + int mid = (left + right) / 2; |
| 124 | + if (nums[mid] == target) |
| 125 | + return mid; |
| 126 | + else if (nums[left] <= nums[mid]) { |
| 127 | + if (nums[left] <= target && target < nums[mid]) |
| 128 | + right = mid - 1; |
| 129 | + else |
| 130 | + left = mid + 1; |
| 131 | + } else { |
| 132 | + if (nums[mid] < target && target <= nums[right]) |
| 133 | + left = mid + 1; |
| 134 | + else |
| 135 | + right = mid - 1; |
| 136 | + } |
| 137 | + } |
| 138 | + return -1; |
| 139 | + } |
| 140 | +}; |
| 141 | +``` |
| 142 | +
|
| 143 | +### Conclusion |
| 144 | +
|
| 145 | +The above solution effectively searches for a target element in a rotated sorted array with distinct values using the binary search algorithm with $O(log n)$ runtime complexity. It handles all edge cases and constraints specified in the problem statement. |
0 commit comments