> For the complete documentation index, see [llms.txt](https://code-snippets.hbamithkumara.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-snippets.hbamithkumara.com/leetcode/problems/101-200/find-minimum-in-rotated-sorted-array-ii.md).

# 154. Find Minimum in Rotated Sorted Array II

### Description

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e.,  `[0,1,2,4,5,6,7]` might become  `[4,5,6,7,0,1,2]`).

Find the minimum element.

The array may contain duplicates.

**Note:**

* This is a follow up problem to [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/).
* Would allow duplicates affect the run-time complexity? How and why?

### Constraints

### Approach

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)
* [ProgramCreek](https://www.programcreek.com/2014/03/leetcode-find-minimum-in-rotated-sorted-array-ii-java/)
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}
**Input:** \[1, 3, 5]

**Output:** 1
{% endtab %}

{% tab title="Example 2" %}
**Input:** \[2, 2, 2, 0, 1]

**Output:** 0
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="Solution 1" %}

```java
/**
 * Time complexity : O(N)
 * Space complexity : O(1)
 */

class Solution {
    public int findMin(int[] nums) {
        if(nums == null || nums.length == 0) return 0;
        
        int n = nums.length;
        int i = 1;
        
        while(i < n && nums[i-1] <= nums[i]) {
            i++;
        }
        
        if(i >= n) return nums[0];
        
        return nums[i];
    }
}
```

{% endtab %}

{% tab title="Solution 2" %}

```java
/**
 * Time complexity : O(logN)
 * Space complexity : O(1)
 */

class Solution {
    public int findMin(int[] nums) {
        if(nums == null || nums.length == 0) return 0;
        
        int left = 0, right = nums.length-1;
        
        while(left <= right) {
            while(left != right && nums[left] == nums[right]) {
                left++;
            }
            
            if(nums[left] <= nums[right]) {
                return nums[left];
            }
            
            int mid = (left+right)/2;
            if(nums[mid] >= nums[left]) {
                left = mid+1;
            } else {
                right = mid;
            }
        }
        
        return -1;
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*
