Loading content...
You are given an integer array nums that was originally sorted in non-decreasing order. However, before being provided to you, this array was cyclically shifted at an unknown pivot position k (where 0 ≤ k < nums.length). A cyclic shift transforms the array such that elements wrap around from the end to the beginning.
For example, the array [0, 1, 2, 4, 4, 4, 5, 6, 6, 7] cyclically shifted at position 5 becomes [4, 5, 6, 6, 7, 0, 1, 2, 4, 4].
Important: Unlike standard sorted array problems, this array may contain duplicate values. This introduces additional complexity when determining which portion of the array to search.
Given the cyclically shifted array nums and an integer target, your task is to determine whether target exists in the array. Return true if the target is present, or false if it is not.
Your algorithm should minimize the number of comparison operations required to arrive at the answer. While a linear scan would work, consider whether you can leverage the partially sorted nature of the array to achieve better performance in typical cases.
nums = [2,5,6,0,0,1,2]
target = 0trueThe value 0 appears in the array at indices 3 and 4, so we return true.
nums = [2,5,6,0,0,1,2]
target = 3falseThe value 3 does not exist anywhere in the array, so we return false.
nums = [4,5,6,6,7,0,1,2,4,4]
target = 6trueThe value 6 appears at indices 2 and 3 in the array. The cyclic shift doesn't prevent us from locating the target.
Constraints