Problem 705
Search in Rotated Sorted Array II
Medium · 20 pts · Array, Binary Search
Problem
A non-decreasing array, which may contain duplicates, was rotated at an unknown position. Decide whether a target value is present. Binary search still works: at each step one half is sorted, and you check whether the target lies inside it. Duplicates add one difficulty: when the values at the low end, the middle and the high end are all equal you cannot tell which half is sorted, so shrink both ends by one and continue.
Input
The first line contains N and TARGET. The second line contains the N values.
Output
Print YES if the target is in the array, otherwise NO.
Constraints
1 <= N <= 100000; 0 <= values, TARGET <= 10^9
Example input
7 0 2 5 6 0 0 1 2
Example output
YES
Explanation
The array 2 5 6 0 0 1 2 is the sorted array 0 0 1 2 2 5 6 rotated. The target 0 appears at index 3, so the answer is YES.