Search in Rotated Sorted Array II – Binary Search with Duplicates Explained (LeetCode 81)
Search in Rotated Sorted Array II – Binary Search with Duplicates Explained (LeetCode 81)

Search in Rotated Sorted Array II – Binary Search with Duplicates Explained (LeetCode 81)

Learn how to search an element in a rotated sorted array that may contain duplicate values. This guide explains the intuition, challenges caused by duplicates, and a clean Java solution using modified binary search.

12 views
0
0

Try the Question

Before reading the explanation, try solving the problem yourself:

👉 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/

Practicing the problem first helps develop stronger problem-solving intuition, especially for binary search variations.


Problem Statement

You are given an integer array nums that is sorted in non-decreasing order.

Example of a sorted array:

[0,1,2,4,4,4,5,6,6,7]

Before being passed to your function, the array may be rotated at some pivot index k.

After rotation, the structure becomes:

[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]

Example:

Original array
[0,1,2,4,4,4,5,6,6,7]

Rotated at index 5
[4,5,6,6,7,0,1,2,4,4]

You are also given an integer target.

Your task is to determine:

  1. Return true if the target exists in the array
  2. Return false if the target does not exist

The goal is to minimize the number of operations, which suggests using Binary Search.


Example Walkthrough

Example 1

Input

nums = [2,5,6,0,0,1,2]
target = 0

Output

true

Explanation:

0 exists in the array

Example 2

Input

nums = [2,5,6,0,0,1,2]
target = 3

Output

false

Explanation:

3 does not exist in the array


Understanding the Core Challenge

This problem is very similar to the classic problem:

Search in Rotated Sorted Array (LeetCode 33).

However, there is an important difference.

Difference Between the Two Problems

ProblemArray Values
Rotated Sorted ArrayAll elements are distinct
Rotated Sorted Array IIArray may contain duplicates

Duplicates introduce ambiguity during binary search.


Why Duplicates Make the Problem Harder

In the previous problem, we relied on this rule:

If nums[left] <= nums[mid]
→ left half is sorted

But duplicates can break this assumption.

Example:

nums = [1,0,1,1,1]

If:

left = 0
mid = 2
right = 4

Then:

nums[left] = 1
nums[mid] = 1
nums[right] = 1

Here we cannot determine which half is sorted.

This is the main complication introduced by duplicates.


Key Idea to Handle Duplicates

When the values at left, mid, and right are the same, the algorithm cannot decide which half is sorted.

To resolve this situation, we shrink the search space:

left++
right--

This gradually removes duplicate values and allows binary search to continue.


Modified Binary Search Strategy

The algorithm works as follows:

Step 1

Calculate the middle index.

Step 2

If the middle element equals the target:

return true

Step 3

If duplicates block decision-making:

nums[left] == nums[mid] == nums[right]

Then move both pointers inward:

left++
right--

Step 4

Otherwise, determine which half is sorted and apply normal binary search logic.


Java Implementation

class Solution {

public boolean search(int[] nums, int target) {

int l = 0;
int r = nums.length - 1;

while (l <= r) {

int mid = l + (r - l) / 2;

if (nums[mid] == target)
return true;

// Handle duplicates
if (nums[l] == nums[mid] && nums[mid] == nums[r]) {
l++;
r--;
}

// Left half sorted
else if (nums[l] <= nums[mid]) {

if (nums[l] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}

}

// Right half sorted
else {

if (nums[mid] < target && target <= nums[r]) {
l = mid + 1;
} else {
r = mid - 1;
}

}
}

return false;
}
}


Step-by-Step Example

Array:

[2,5,6,0,0,1,2]
target = 0

Iteration 1

mid = 6
value = 0

Target found immediately.

return true


Time Complexity

Best Case

O(log n)

When duplicates do not interfere with binary search decisions.

Worst Case

O(n)

When many duplicate values force the algorithm to shrink the search space one element at a time.

Example worst case:

[1,1,1,1,1,1,1,1,1]

Binary search cannot divide the array effectively.


Space Complexity

O(1)

The algorithm only uses a few variables and does not require extra memory.


Follow-Up: How Do Duplicates Affect Runtime?

Without duplicates, binary search always reduces the search space by half.

Time Complexity → O(log n)

With duplicates, we sometimes cannot determine which half is sorted.

In such cases, we shrink the search space linearly:

left++
right--

This may degrade performance to:

Worst Case → O(n)

However, in most practical cases the algorithm still performs close to logarithmic time.


Key Takeaways

✔ The array is sorted but rotated

✔ Duplicates introduce ambiguity in binary search

✔ Special handling is required when nums[left] == nums[mid] == nums[right]

✔ The algorithm combines binary search with duplicate handling

✔ Worst-case complexity may degrade to O(n)


Final Thoughts

This problem is a natural extension of the rotated sorted array search problem. It tests your ability to adapt binary search to more complex conditions.

Understanding this pattern is valuable because similar techniques appear in many interview problems involving:

  1. Rotated arrays
  2. Binary search edge cases
  3. Handling duplicates in sorted data structures

Mastering this approach strengthens both algorithmic thinking and interview preparation.

Ai Assistant Kas