Skip to content

Commit c27edcd

Browse files
committed
doc: add documentation and README
1 parent 300f9bf commit c27edcd

File tree

3 files changed

+173
-14
lines changed

3 files changed

+173
-14
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
Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,55 @@
11
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+
"""
213
low = 0
314
high = len(nums) - 1
415

516
while low <= high:
6-
mid = low + (high - low)//2
17+
mid = low + (high - low) // 2
18+
19+
# Check if the mid element is the target
720
if nums[mid] == target:
821
return mid
922

23+
# Determine if the left half is sorted
1024
if nums[low] <= nums[mid]:
11-
if nums[low] <= target and target <= nums[mid]:
25+
# Check if the target is in the sorted left half
26+
if nums[low] <= target <= nums[mid]:
1227
high = mid - 1
1328
else:
1429
low = mid + 1
15-
1630
else:
17-
if nums[mid] <= target and target <= nums[high]:
31+
# Check if the target is in the sorted right half
32+
if nums[mid] <= target <= nums[high]:
1833
low = mid + 1
1934
else:
2035
high = mid - 1
21-
return -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: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
1-
def search_rotated_sorted_array(nums: list[int],low: int, high: int, target:int) -> int:
2-
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+
"""
315
if low >= high:
416
return False
517

6-
mid = low + (high - low)//2
18+
mid = low + (high - low) // 2
719

820
if nums[mid] == target:
921
return True
1022

23+
# Check if the left half is sorted
1124
if nums[low] <= nums[mid]:
12-
if nums[low] <= target and target <= nums[mid]:
13-
return search_rotated_sorted_array(nums, low, mid-1, target)
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)
1428
else:
15-
return search_rotated_sorted_array(nums, mid+1, high, target)
29+
return search_rotated_sorted_array(nums, mid + 1, high, target)
1630
else:
17-
if nums[mid] <= target and target <= nums[high]:
18-
return search_rotated_sorted_array(nums, mid+1, high, target)
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)
1934
else:
20-
return search_rotated_sorted_array(nums, low, mid-1, target)
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)