Search in Rotated Sorted Array II
Problem Statement
This is Search in Rotated Sorted Array, but now the values may repeat.
The array nums was sorted from smallest to largest (duplicates allowed) and then possibly rotated: cut at some index and the front part moved to the back. For example, [0, 1, 2, 4, 4, 4, 5, 6, 6, 7] can become [4, 5, 6, 6, 7, 0, 1, 2, 4, 4].
Given nums and a number target, return true if target is in nums, otherwise false. Try to do as few checks as you can.
A rotated sorted array nums that may contain duplicates, and an integer target.
true if target appears in nums, otherwise false.
0 appears at indexes 3 and 4.
3 is not in the array.
nums[lo], nums[mid] and nums[hi] are all 1 at the start, so you can't tell where the drop is. This is the case that breaks the LC 33 code.
- β’ 1 β€ n β€ 5000
- β’ β10^4 β€ nums[i] β€ 10^4
- β’ nums is a sorted array (duplicates allowed) that may have been rotated.
- β’ β10^4 β€ target β€ 10^4
- β’ Follow-up: how do duplicates change the running time?
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Check every element for target
LC 33's sorted-half check; when nums[lo] == nums[mid] == nums[hi], do lo++, hi--
Walk the whole array and return true as soon as you see target.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.