Loading problem...
You are given an integer array nums that was originally sorted in non-decreasing order and may contain duplicate values. This array has been circularly shifted (rotated) between 1 and n times, where n is the length of the array.
A circular shift (or rotation) on an array [a[0], a[1], a[2], ..., a[n-1]] moves the last element to the front, resulting in [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
For example, if the original sorted array with duplicates is nums = [0, 1, 4, 4, 5, 6, 7], after various rotations it could become:
[4, 5, 6, 7, 0, 1, 4] after 4 rotations[0, 1, 4, 4, 5, 6, 7] after 7 rotations (returns to original)Your task is to locate and return the smallest element in this rotated array.
Important Constraint: The array may contain duplicate values, which adds complexity to the search. Design an algorithm that minimizes the number of operations required to find the minimum element, even in the presence of duplicates.
nums = [1, 3, 5]1The array appears unrotated or rotated by a multiple of its length. The smallest element is 1 at index 0.
nums = [2, 2, 2, 0, 1]0The array was rotated such that the minimum element (0) is now at index 3. Despite multiple duplicate values of 2, the algorithm correctly identifies 0 as the smallest element.
nums = [4, 5, 6, 7, 0, 1, 4]0This array contains duplicates (4 appears twice) and was rotated 4 times. The rotation point is between indices 3 and 4, where the smallest element 0 is located.
Constraints