Practice › Binary Search
Search in a Rotated Array with Duplicates
Medium · Binary Search · Asked at Amazon, Microsoft, Meta, LinkedIn
A non-decreasing array (which may contain duplicates) was rotated at some unknown point, e.g. [0, 0, 1, 2, 2, 5, 6] → [2, 5, 6, 0, 0, 1, 2]. Print YES if target is in the array, otherwise NO.
Input
First line: n. Second line: n space-separated integers. Third line: target.
Output
YES or NO.
Constraints
- 1 ≤ n ≤ 10^5
- −10^4 ≤ values, target ≤ 10^4
Binary search as for a rotated array: one half around mid is always sorted, so check whether target lies in it. When a[lo] = a[mid] = a[hi], you cannot tell which half is sorted — shrink both ends by one. This makes the worst case O(n).
Example 1
Input
7 2 5 6 0 0 1 2 0
Output
YES
Example 2
Input
7 2 5 6 0 0 1 2 3
Output
NO
Hints
- In a rotated sorted array, at least one of the halves around mid is sorted.
- If target lies within the sorted half’s range, search there; otherwise search the other half.
- Duplicates can hide which half is sorted: when a[lo] = a[mid] = a[hi], shrink both ends.
Approach
Standard rotated-array binary search with one extra case. If a[mid] == target, done. If a[lo] == a[mid] == a[hi], move lo up and hi down by one (we cannot decide, but those ends are not the target). Else if a[lo] ≤ a[mid], the left half is sorted: go left when a[lo] ≤ target < a[mid], else right. Otherwise the right half is sorted: go right when a[mid] < target ≤ a[hi], else left. With many duplicates this degrades to O(n), which is unavoidable.
Complexity: O(log n) average, O(n) worst case · O(1) space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (auto &x : a) cin >> x;
int t;
cin >> t;
int lo = 0, hi = n - 1;
bool found = false;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (a[mid] == t) {
found = true;
break;
}
if (a[lo] == a[mid] && a[mid] == a[hi]) {
lo++;
hi--;
} else if (a[lo] <= a[mid]) {
if (a[lo] <= t && t < a[mid]) hi = mid - 1;
else lo = mid + 1;
} else {
if (a[mid] < t && t <= a[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
cout << (found ? "YES" : "NO") << "\n";
return 0;
}Python solution
import sys
data = list(map(int, sys.stdin.read().split()))
n = data[0]
a = data[1 : 1 + n]
t = data[1 + n]
lo, hi = 0, n - 1
found = False
while lo <= hi:
mid = (lo + hi) // 2
if a[mid] == t:
found = True
break
if a[lo] == a[mid] == a[hi]:
lo += 1
hi -= 1
elif a[lo] <= a[mid]:
if a[lo] <= t < a[mid]:
hi = mid - 1
else:
lo = mid + 1
else:
if a[mid] < t <= a[hi]:
lo = mid + 1
else:
hi = mid - 1
print("YES" if found else "NO")Java solution
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
int[] a = new int[n];
for (int i = 0; i < n; i++) {
in.nextToken();
a[i] = (int) in.nval;
}
in.nextToken();
int t = (int) in.nval;
int lo = 0, hi = n - 1;
boolean found = false;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (a[mid] == t) {
found = true;
break;
}
if (a[lo] == a[mid] && a[mid] == a[hi]) {
lo++;
hi--;
} else if (a[lo] <= a[mid]) {
if (a[lo] <= t && t < a[mid]) hi = mid - 1;
else lo = mid + 1;
} else {
if (a[mid] < t && t <= a[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
System.out.println(found ? "YES" : "NO");
}
}Solve Search in a Rotated Array with Duplicates online · All practice problems