LeetCode 81: Search in Rotated Sorted Array II Solution

Master LeetCode problem 81 (Search in Rotated Sorted Array II), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

81. Search in Rotated Sorted Array II

Problem Explanation

Explanation

To solve this problem efficiently, we can modify the binary search algorithm. The idea is to check which part of the rotated array is sorted and then perform binary search on that part. We need to handle the case where there are duplicates in the array.

  1. Initialize two pointers, left at index 0 and right at index nums.length - 1.
  2. While left <= right, calculate the middle index mid.
  3. If nums[mid] == target, return true.
  4. If nums[left] == nums[mid], increment left and continue the loop.
  5. Check if the left half (nums[left] <= nums[mid]) is sorted:
    • If nums[left] <= nums[mid] and nums[left] <= target < nums[mid], update right = mid - 1.
    • Otherwise, update left = mid + 1.
  6. If the left half is not sorted, the right half must be sorted:
    • If nums[mid] < target <= nums[right], update left = mid + 1.
    • Otherwise, update right = mid - 1.
  7. If target is not found after the loop, return false.

Time complexity: O(log n)
Space complexity: O(1)

Solution Code

class Solution {
    public boolean search(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return true;
            }
            
            if (nums[left] == nums[mid]) {
                left++;
            } else if (nums[left] <= nums[mid]) {
                if (nums[left] <= target && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[right]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return false;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 81 (Search in Rotated Sorted Array II)?

This page provides optimized solutions for LeetCode problem 81 (Search in Rotated Sorted Array II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 81 (Search in Rotated Sorted Array II)?

The time complexity for LeetCode 81 (Search in Rotated Sorted Array II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 81 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 81 in Java, C++, or Python.

Back to LeetCode Solutions