Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f3b04eb

Browse files
Merge pull request #365 from MoigeMatino/add-search-rotated-array
feat: add search rotated array
2 parents f441299 + c27edcd commit f3b04eb

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## **Problem Statement**
2+
3+
Given a sorted integer array, `nums`, and an integer value, `target`, the array is rotated by some arbitrary number. Search and return the index of `target` in this array. If the `target` does not exist, return `-1`.
4+
5+
6+
### Constraints
7+
- All values in nums are unique.
8+
- The values in nums are sorted in ascending order.
9+
- The array may have been rotated by some arbitrary number.
10+
- 1 ≤ nums.length ≤1000≤1000
11+
- −10<sup>4</sup> ≤ nums[i] ≤ 10<sup>4</sup>
12+
- −10<sup>4</sup> ≤ target ≤ 10<sup>4</sup>
13+
14+
## **Examples**
15+
16+
### Example 1: Target Found in Rotated Array
17+
18+
**Input:**
19+
20+
- `nums = [4, 5, 6, 7, 0, 1, 2]`
21+
- `target = 0`
22+
23+
**Process:**
24+
- The array `[4, 5, 6, 7, 0, 1, 2]` is a sorted array that has been rotated.
25+
- The target value `0` is located at index `4`.
26+
27+
**Output:** `4`
28+
29+
**Visualization:**
30+
- Original Sorted Array: [0, 1, 2, 4, 5, 6, 7]
31+
- Rotated Array: [4, 5, 6, 7, 0, 1, 2]
32+
- Target: 0
33+
- Index of Target: 4
34+
35+
### Example 2: Target Not Found
36+
37+
**Input:**
38+
- `nums = [4, 5, 6, 7, 0, 1, 2]`
39+
- `target = 3`
40+
41+
**Process:**
42+
- The array `[4, 5, 6, 7, 0, 1, 2]` is a sorted array that has been rotated.
43+
- The target value `3` is not present in the array.
44+
45+
**Output:** `-1`
46+
47+
**Visualization:**
48+
49+
Original Sorted Array: [0, 1, 2, 4, 5, 6, 7]
50+
Rotated Array: [4, 5, 6, 7, 0, 1, 2]
51+
Target: 3
52+
Index of Target: -1 (not found)
53+
54+
55+
### Example 3: Target Found at Beginning
56+
57+
**Input:**
58+
- `nums = [6, 7, 0, 1, 2, 4, 5]`
59+
- `target = 6`
60+
61+
**Process:**
62+
- The array `[6, 7, 0, 1, 2, 4, 5]` is a sorted array that has been rotated.
63+
- The target value `6` is located at index `0`.
64+
65+
**Output:** `0`
66+
67+
**Visualization:**
68+
69+
- Original Sorted Array: [0, 1, 2, 4, 5, 6, 7]
70+
- Rotated Array: [6, 7, 0, 1, 2, 4, 5]
71+
- Target: 6
72+
- Index of Target: 0
73+
74+
75+
### Example 4: Target Found at End
76+
77+
**Input:**
78+
- `nums = [6, 7, 0, 1, 2, 4, 5]`
79+
- `target = 5`
80+
81+
**Process:**
82+
- The array `[6, 7, 0, 1, 2, 4, 5]` is a sorted array that has been rotated.
83+
- The target value `5` is located at index `6`.
84+
85+
**Output:** `6`
86+
87+
**Visualization:**
88+
89+
- Original Sorted Array: [0, 1, 2, 4, 5, 6, 7]
90+
- Rotated Array: [6, 7, 0, 1, 2, 4, 5]
91+
- Target: 5
92+
- Index of Target: 6

‎arrays_and_strings/search_rotated_sorted_array/__init__.py

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
def search_rotated_sorted_array(nums: list[int], target: int) -> int:
2+
"""
3+
Search for a target value in a rotated sorted array using an iterative binary search.
4+
5+
Parameters:
6+
nums (list[int]): The rotated sorted array.
7+
target (int): The target value to search for.
8+
9+
Returns:
10+
int: The index of the target if found, otherwise -1.
11+
12+
"""
13+
low = 0
14+
high = len(nums) - 1
15+
16+
while low <= high:
17+
mid = low + (high - low) // 2
18+
19+
# Check if the mid element is the target
20+
if nums[mid] == target:
21+
return mid
22+
23+
# Determine if the left half is sorted
24+
if nums[low] <= nums[mid]:
25+
# Check if the target is in the sorted left half
26+
if nums[low] <= target <= nums[mid]:
27+
high = mid - 1
28+
else:
29+
low = mid + 1
30+
else:
31+
# Check if the target is in the sorted right half
32+
if nums[mid] <= target <= nums[high]:
33+
low = mid + 1
34+
else:
35+
high = mid - 1
36+
37+
# Target not found
38+
return -1
39+
40+
# Approach and Rationale:
41+
# -----------------------
42+
# - This function uses an iterative binary search approach to find the target in a rotated sorted array.
43+
# - A rotated sorted array is an array that was originally sorted in increasing order but then rotated at some pivot.
44+
# - The key observation is that at least one half of the array (either left or right of the midpoint) is always sorted.
45+
# - By comparing the target with the elements in the sorted half, we can decide which half to continue searching in.
46+
# - This approach reduces the search space by half in each iteration, similar to binary search.
47+
48+
# Time Complexity:
49+
# ----------------
50+
# - The time complexity is O(log n), where n is the number of elements in the array.
51+
# - This is because the search space is halved in each iteration, similar to binary search.
52+
53+
# Space Complexity:
54+
# -----------------
55+
# - The space complexity is O(1) because the algorithm uses a constant amount of extra space.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
def search_rotated_sorted_array(nums: list[int], low: int, high: int, target: int) -> int:
2+
"""
3+
Search for a target value in a rotated sorted array using a modified binary search.
4+
5+
Parameters:
6+
nums (list[int]): The rotated sorted array.
7+
low (int): The starting index of the search range.
8+
high (int): The ending index of the search range.
9+
target (int): The target value to search for.
10+
11+
Returns:
12+
int: True if the target is found, False otherwise.
13+
14+
"""
15+
if low >= high:
16+
return False
17+
18+
mid = low + (high - low) // 2
19+
20+
if nums[mid] == target:
21+
return True
22+
23+
# Check if the left half is sorted
24+
if nums[low] <= nums[mid]:
25+
# If target is in the sorted left half
26+
if nums[low] <= target <= nums[mid]:
27+
return search_rotated_sorted_array(nums, low, mid - 1, target)
28+
else:
29+
return search_rotated_sorted_array(nums, mid + 1, high, target)
30+
else:
31+
# If target is in the sorted right half
32+
if nums[mid] <= target <= nums[high]:
33+
return search_rotated_sorted_array(nums, mid + 1, high, target)
34+
else:
35+
return search_rotated_sorted_array(nums, low, mid - 1, target)
36+
37+
# Approach and Rationale:
38+
# -----------------------
39+
# - The function uses a recursive binary search approach to find the target in a rotated sorted array.
40+
# - A rotated sorted array is an array that was originally sorted in increasing order but then rotated at some pivot.
41+
# - The key observation is that at least one half of the array (either left or right of the midpoint) is always sorted.
42+
# - By comparing the target with the elements in the sorted half, we can decide which half to continue searching in.
43+
# - This approach reduces the search space by half in each recursive call, similar to binary search.
44+
45+
# Time Complexity:
46+
# ----------------
47+
# - The time complexity is O(log n), where n is the number of elements in the array.
48+
# - This is because the search space is halved in each recursive call, similar to binary search.
49+
50+
# Space Complexity:
51+
# -----------------
52+
# - The space complexity is O(log n) due to the recursive call stack.
53+
# - Each recursive call adds a new layer to the call stack, and the maximum depth of recursion is proportional to log n.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /