Search in Rotated Sorted Array in Python and Java
In this tutorial, we will learn LeetCode #33: Search in Rotated Sorted Array in very simple language. We will understand the idea step by step, see custom examples, and write complete code in Python and Java.
What Is the Search in Rotated Sorted Array Problem?
This problem asks us to solve a common coding interview task using the given input. The goal is to return the correct result without using a slow brute force method.
For this problem, we will use binary search because it gives a clean and optimized solution.
Example input: nums = [11, 14, 18, 2, 5, 8], target = 5 Expected result: Output: 4 Explanation: Target 5 is at index 4.
Beginner-Friendly Idea
The main idea is to avoid trying every possible answer blindly. Instead, we keep useful information while reading the input and use that information to make the next decision.
At each step, ask: “What do I already know, and how does the current value change my answer?”
Using our example: nums = [11, 14, 18, 2, 5, 8], target = 5 Approach: binary search We update variables step by step until we reach: Output: 4
Why Do We Use This Approach?
A direct brute force solution is usually easier to think about, but it can become slow when the input is large. The optimized approach keeps only the important state and avoids repeated work.
That is why binary search is useful for this problem.
Step-by-Step Explanation
Let us dry run the algorithm using a custom example.
Step 1 Use this input. nums = [11, 14, 18, 2, 5, 8] target = 5 left = 0 right = 5 Step 2 Find the middle index. mid = (0 + 5) // 2 mid = 2 nums[mid] = 18 Step 3 The left side is sorted because nums[left] <= nums[mid]. nums[left] = 11 nums[mid] = 18 Target 5 is not between 11 and 18. So we move right. left = mid + 1 left = 3 Step 4 Now search from index 3 to 5. mid = (3 + 5) // 2 mid = 4 nums[mid] = 5 Step 5 nums[mid] equals target. return 4
Important Code Logic
The most important part is updating the algorithm state after reading each useful value. This is where the answer becomes better step by step.
Think like this: old_state = what we knew before current_value = value we are checking now new_state = updated result after using current_value For our example, the final state gives: Output: 4
Example 1
Input: nums = [11, 14, 18, 2, 5, 8], target = 5 Output: Output: 4 Explanation: Target 5 is at index 4.
Example 2
Input: nums = [30, 40, 50, 5, 10, 20], target = 10 Output: 4 Explanation: Target 10 is found at index 4.
Python Code
Here is the complete Python solution for LeetCode #33.
class RotatedArrayTargetSearcher:
def search(self, nums, target):
left = 0
right = len(nums) - 1
while left <= right:
middle = (left + right) // 2
if nums[middle] == target:
return middle
if nums[left] <= nums[middle]:
if nums[left] <= target < nums[middle]:
right = middle - 1
else:
left = middle + 1
else:
if nums[middle] < target <= nums[right]:
left = middle + 1
else:
right = middle - 1
return -1
searcher = RotatedArrayTargetSearcher()
print(searcher.search([11, 14, 18, 2, 5, 8], 5)) # Output: 4
print(searcher.search([6, 7, 1, 2, 3, 4], 9)) # Output: -1Java Code
Here is the complete Java solution for LeetCode #33.
class RotatedArrayTargetSearcher {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int middle = left + (right - left) / 2;
if (nums[middle] == target) {
return middle;
}
if (nums[left] <= nums[middle]) {
if (nums[left] <= target && target < nums[middle]) {
right = middle - 1;
} else {
left = middle + 1;
}
} else {
if (nums[middle] < target && target <= nums[right]) {
left = middle + 1;
} else {
right = middle - 1;
}
}
}
return -1;
}
public static void main(String[] args) {
RotatedArrayTargetSearcher searcher = new RotatedArrayTargetSearcher();
System.out.println(searcher.search(new int[] {11, 14, 18, 2, 5, 8}, 5)); // Output: 4
System.out.println(searcher.search(new int[] {6, 7, 1, 2, 3, 4}, 9)); // Output: -1
}
}Time and Space Complexity
Time Complexity: O(log n)
The time complexity depends on how many values the algorithm needs to process and whether it uses sorting, binary search, heap, or traversal.
Space Complexity: O(1)
The extra space is used for the variables or data structures needed by the optimized approach.
Final Summary
LeetCode #33: Search in Rotated Sorted Array becomes easier when we break it into small steps. First understand what the problem asks, then track the important state, dry run with an example, and finally write the code.