# 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**

*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://code-snippets.hbamithkumara.com/leetcode/problems/101-200/find-minimum-in-rotated-sorted-array-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
