Skip to content

Commit bf39427

Browse files
Added solution for search-in-rotated-sorted-array
1 parent 01a34c6 commit bf39427

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
id: search-in-rotated-sorted-array
3+
title: Search in Rotated Sorted Array (LeetCode)
4+
difficulty: Medium
5+
sidebar_label: 0033-SearchInRotatedSortedArray
6+
topics:
7+
- Array
8+
- Binary Search
9+
companies: []
10+
description: Search for a target element in a rotated sorted array with distinct values using an algorithm with O(log n) runtime complexity.
11+
---
12+
13+
## Problem Description
14+
15+
16+
| Problem Statement | Solution Link | LeetCode Profile |
17+
| :---------------- | :------------ | :--------------- |
18+
| [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/) |
19+
20+
## Problem Description
21+
22+
There is an integer array `nums` sorted in ascending order (with distinct values).
23+
24+
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).
25+
26+
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`.
27+
28+
You must write an algorithm with O(log n) runtime complexity.
29+
30+
### Examples
31+
32+
#### Example 1
33+
34+
- **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 0`
35+
- **Output:** `4`
36+
- **Explanation:** 0 is located at index 4 in the rotated sorted array `[4,5,6,7,0,1,2]`.
37+
38+
#### Example 2
39+
40+
- **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 3`
41+
- **Output:** `-1`
42+
- **Explanation:** 3 is not in `nums`, so return -1.
43+
44+
#### Example 3
45+
46+
- **Input:** `nums = [1]`, `target = 0`
47+
- **Output:** `-1`
48+
- **Explanation:** 0 is not in `nums`, so return -1.
49+
50+
### Constraints
51+
52+
- `1 <= nums.length <= 5000`
53+
- `-10^4 <= nums[i] <= 10^4`
54+
- All values of `nums` are unique.
55+
- `nums` is an ascending array that is possibly rotated.
56+
- `-10^4 <= target <= 10^4`
57+
58+
### Approach
59+
60+
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.
61+
62+
1. **Find the Pivot Point:**
63+
- 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.
64+
65+
2. **Perform Binary Search:**
66+
- Based on the pivot element, determine which half of the array the target might be in.
67+
- Perform binary search in the appropriate half to find the target element.
68+
69+
### Solution Code
70+
71+
#### Python
72+
73+
```
74+
class Solution:
75+
def search(self, nums: List[int], target: int) -> int:
76+
left, right = 0, len(nums) - 1
77+
while left <= right:
78+
mid = (left + right) // 2
79+
if nums[mid] == target:
80+
return mid
81+
elif nums[left] <= nums[mid]:
82+
if nums[left] <= target < nums[mid]:
83+
right = mid - 1
84+
else:
85+
left = mid + 1
86+
else:
87+
if nums[mid] < target <= nums[right]:
88+
left = mid + 1
89+
else:
90+
right = mid - 1
91+
return -1
92+
```
93+
94+
#### Java
95+
96+
```
97+
class Solution {
98+
public int search(int[] nums, int target) {
99+
int left = 0, right = nums.length - 1;
100+
while (left <= right) {
101+
int mid = (left + right) / 2;
102+
if (nums[mid] == target)
103+
return mid;
104+
else if (nums[left] <= nums[mid]) {
105+
if (nums[left] <= target && target < nums[mid])
106+
right = mid - 1;
107+
else
108+
left = mid + 1;
109+
} else {
110+
if (nums[mid] < target && target <= nums[right])
111+
left = mid + 1;
112+
else
113+
right = mid - 1;
114+
}
115+
}
116+
return -1;
117+
}
118+
}
119+
```
120+
121+
#### C++
122+
123+
```
124+
class Solution {
125+
public:
126+
int search(vector<int>& nums, int target) {
127+
int left = 0, right = nums.size() - 1;
128+
while (left <= right) {
129+
int mid = (left + right) / 2;
130+
if (nums[mid] == target)
131+
return mid;
132+
else if (nums[left] <= nums[mid]) {
133+
if (nums[left] <= target && target < nums[mid])
134+
right = mid - 1;
135+
else
136+
left = mid + 1;
137+
} else {
138+
if (nums[mid] < target && target <= nums[right])
139+
left = mid + 1;
140+
else
141+
right = mid - 1;
142+
}
143+
}
144+
return -1;
145+
}
146+
};
147+
```
148+
149+
### Conclusion
150+
151+
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

Comments
 (0)