Leetcode 33 - Search in Rotated Sorted Array
Understanding the problem
LeetCode 33 is a problem where you are given a sorted array of integers that has been rotated by some number of elements, and a target value, and you need to find the target value in the array. Here is an example:
Input: nums = [4,5,6,7,0,1,2] target = 0
Output: 4
In this example, the array [4, 5, 6, 7, 0, 1, 2] was rotated 4 times to the right, so the element at index 0 is now at index 4.
Grow Your Tech Career. Meet Expert coaches from top companies
Plan your solution
To solve this problem, we can use a modified version of binary search. Since the array is sorted, we can use binary search to find the target value. However, since the array has been rotated, we need to consider the following two cases:
- If the middle element is greater than the element at the start of the array, then the start of the array is in the sorted half of the array.
- If the middle element is less than the element at the start of the array, then the end of the array is in the sorted half of the array.
We can use this information to determine which half of the array to search. If the target value is in the sorted half of the array, we can use binary search to find it. If the target value is not in the sorted half of the array, we can search the other half of the array.
Implement your solution
Here is the implementation of the search
function:
class Solution:
def search(self, nums: List[int], target: int) -> int:
# Set the start and end indices for the search
start = 0
end = len(nums) - 1
# Loop until the start and end indices meet or cross
while start <= end:
# Calculate the middle index
mid = (start + end) // 2
# If the target value is equal to the value at the middle index, return the middle index
if nums[mid] == target:
return mid
# If the middle element is greater than the element at the start of the array, the start of the array is in the sorted half
if nums[mid] > nums[start]:
# If the target value is in the sorted half, search the left half of the array
if target >= nums[start] and target < nums[mid]:
end = mid - 1
# If the target value is not in the sorted half, search the right half of the array
else:
start = mid + 1
# If the middle element is less than the element at the start of the array, the end of the array is in the sorted half
else:
# If the target value is in the sorted half, search the right half of the array
if target > nums[mid] and target <= nums[end]:
start = mid + 1
# If the target value is not in the sorted half, search the left half of the array
else:
end = mid - 1
return -1
Test your solution
Five test cases that you can use to test the search
function for solving LeetCode 33, the “Search in Rotated Sorted Array” problem.
# Test case 1
nums = [4, 5, 6, 7, 0, 1, 2]
target = 0
expected_output = 4
assert Solution().search(nums, target) == expected_output
# Test case 2
nums = [4, 5, 6, 7, 0, 1, 2]
target = 2
expected_output = 6
assert Solution().search(nums, target) == expected_output
# Test case 3
nums = [4, 5, 6, 7, 0, 1, 2]
target = 4
expected_output = 0
assert Solution().search(nums, target) == expected_output
# Test case 4
nums = [4, 5, 6, 7, 0, 1, 2]
target = 3
expected_output = -1
assert Solution().search(nums, target) == expected_output
# Test case 5
nums = [4, 5, 6, 7, 0, 1, 2]
target = 8
expected_output = -1
assert Solution().search(nums, target) == expected_output
Grow Your Tech Career. Meet Expert coaches from top companies
Related:
- search a 2d matrix ii
- single element in a sorted array
- count negative numbers in a sorted matrix
- binary search
- unique binary search trees
Related Posts
- Leetcode 424 - Longest Repeating Character Replacement
- Two Sum and Its Variants
- Leetcode 1004 - Max Consecutive Ones III
- Leetcode 222 - Count Complete Tree Nodes
- Leetcode 1027 - Longest Arithmetic Subsequence
- Question 1299 on leetcode
- Leetcode 240 - Search a 2D Matrix II
- Leetcode 1351 - Count Negative Numbers in a Sorted Matrix
- Leetcode 239 - Sliding Window Maximum
- Leetcode 209 - Minimum Size Subarray Sum
- LeetCode 230 - Kth Smallest Element in a BST with Code
- advanced-applications-of-binary-search
- Leetcode 96 - Unique Binary Search Trees
- Leetcode 540 - Single Element in a Sorted Array
- Leetcode 1283 - Find the Smallest Divisor Given a Threshold
- Leetcode 74 - Search a 2D Matrix
- Leetcode 300 - Longest Increasing Subsequence
- Leetcode 930 - Binary Subarrays With Sum
- Blazingly fast solution to LeetCode #1342 - Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold (Cross-post from r/SoftwareEngineering)
- Leetcode 704 - Binary Search
- Leetcode 643 - Maximum Average Subarray I
- LeetCode - Two Sum Solution with Code
- a-binary-search-template
- Leetcode 1358 - Number of Substrings Containing All Three Characters
- Leetcode 1151 - Minimum Swaps to Group All 1’s Together