## Algorithm

Problem Name: 154. Find Minimum in Rotated Sorted Array II

Suppose an array of length `n` sorted in ascending order is rotated between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become:

• `[4,5,6,7,0,1,4]` if it was rotated `4` times.
• `[0,1,4,4,5,6,7]` if it was rotated `7` times.

Notice that rotating an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.

Given the sorted rotated array `nums` that may contain duplicates, return the minimum element of this array.

You must decrease the overall operation steps as much as possible.

Example 1:

```Input: nums = [1,3,5]
Output: 1
```

Example 2:

```Input: nums = [2,2,2,0,1]
Output: 0
```

Constraints:

• `n == nums.length`
• `1 <= n <= 5000`
• `-5000 <= nums[i] <= 5000`
• `nums` is sorted and rotated between `1` and `n` times.

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
#include <stdio.h>
#include <assert.h>

int findMin(int *nums, int numsSize) {
int l = 0;
int r = numsSize - 1;
while (l  <  r) {
int m = l + (r - l) / 2;
if (nums[l] > nums[m]) { /* right side is sorted */
r = m;
}
else if (nums[r]  <  nums[m]) { /* left side is sorted */
l = m + 1;
}
else { /* the sub-array is not rotated, 1 step to deal with dups */
r--;
}
}
return nums[l];
}

int main() {
int nums[] = { 3, 3, 1, 3 };

assert(findMin(nums, sizeof(nums) / sizeof(nums[0])) == 1);

printf("all tests passed!\n");

return 0;
}
``````
Copy The Code &

Input

cmd
nums = [1,3,5]

Output

cmd
1

### #2 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public int findMin(int[] nums) {
int start = 0;
int end = nums.length - 1;
while (start  < = end) {
int mid = start + (end - start) / 2;
if (nums[mid] < nums[end]) {
end = mid;
}
else if (nums[mid] > nums[end]) {
start = mid + 1;
}
else {
end--;
}
}
return nums[start];
}
}
``````
Copy The Code &

Input

cmd
nums = [1,3,5]

Output

cmd
1

### #3 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const findMin = function(nums) {
for(let i = 1, len = nums.length; i  <  len; i++) {
if(nums[i] < nums[i - 1]> {
return nums[i]
}
}
return nums[0]
};
``````
Copy The Code &

Input

cmd
nums = [2,2,2,0,1]

### #4 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def findMin(self, nums):
l, r, res = 0, len(nums) - 1, nums and nums[0]
while l <= r:
while l < r and nums[l] == nums[l + 1]: l += 1
while l < r and nums[r] == nums[r - 1]: r -= 1
mid = (l + r) // 2
if nums[mid] >= nums[0]: l = mid + 1
else: r, res = mid - 1, min(res, nums[mid])
return res
``````
Copy The Code &

Input

cmd
nums = [2,2,2,0,1]

### #5 Code Example with C# Programming

```Code - C# Programming```

``````
namespace LeetCode
{
public class _0154_FindMinimumInRotatedSortedArrayII
{
public int FindMin(int[] nums)
{
if (nums.Length == 1) return nums[0];
if (nums[nums.Length - 1] > nums[0]) return nums[0];

int left = 0, right = nums.Length - 1;
while (left  <  right)
{
var mid = left + (right - left) / 2;

if (nums[mid] < nums[right]) right = mid;
else if (nums[mid] > nums[right]) left = mid + 1;
else right--;
}

return nums[right];
}
}
}
``````
Copy The Code &

Input

cmd
nums = [1,3,5]

Output

cmd
1