Skip to content

Commit f8db4c5

Browse files
authored
Merge pull request #1450 from PradnyaGaitonde/PradnyaGaitonde-patch-9
Create 0153-find-minimum-in-rotated-sorted-array.md
2 parents 0591eaf + 1741749 commit f8db4c5

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
id: find-minimum-in-rotated-sorted-array
3+
title: Find Minimum in Rotated Sorted Array(LeetCode)
4+
sidebar_label: 0153-Find Minimum in Rotated Sorted Array
5+
tags:
6+
- Array
7+
- Binary Search
8+
description: Given the sorted rotated array nums of unique elements, return the minimum element of this array.
9+
---
10+
11+
## Problem Statement
12+
13+
Suppose an array of length `n` sorted in ascending order is rotated between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become:
14+
15+
`[4,5,6,7,0,1,2]` if it was rotated 4 times.
16+
`[0,1,2,4,5,6,7]` if it was rotated 7 times.
17+
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]]`.
18+
19+
Given the sorted rotated array `nums` of unique elements, return the minimum element of this array.
20+
21+
You must write an algorithm that runs in `O(log n) time`.
22+
23+
### Examples
24+
25+
**Example 1:**
26+
27+
```plaintext
28+
Input: nums = [3,4,5,1,2]
29+
Output: 1
30+
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
31+
```
32+
33+
**Example 2:**
34+
35+
```plaintext
36+
Input: nums = [4,5,6,7,0,1,2]
37+
Output: 0
38+
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
39+
```
40+
41+
**Example 3:**
42+
43+
```plaintext
44+
Input: nums = [11,13,15,17]
45+
Output: 11
46+
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
47+
```
48+
49+
### Constraints
50+
51+
- `n == nums.length`
52+
- `1 <= n <= 5000`
53+
- `-5000 <= nums[i] <= 5000`
54+
- All the integers of `nums` are unique.
55+
- `nums` is sorted and rotated between `1` and `n` times.
56+
57+
## Solution
58+
59+
This problem aims to find the minimum element in a rotated sorted array. The provided solution uses a binary search approach to efficiently find the minimum element.
60+
61+
### Approach
62+
63+
#### Algorithm
64+
65+
1. Initialize two pointers, `low` at the beginning of the array and `high` at the end of the array.
66+
2. Use a `while` loop with the condition `low < high`:
67+
* Calculate the middle index `mid` as `low + (high - low) / 2`.
68+
* Compare the element at index `mid` with the element at index `high`.
69+
* If `num[mid] < num[high]`, it means the minimum element is in the left part, so update `high = mid`.
70+
* If `num[mid] > num[high]`, it means the minimum element is in the right part, so update `low = mid + 1`.
71+
3. When the `while` loop ends, low will be pointing to the minimum element, so return `num[low]`.
72+
73+
#### Implementation
74+
75+
```C++
76+
class Solution {
77+
public:
78+
int findMin(vector<int> &num) {
79+
int low = 0, high = num.size() - 1;
80+
// loop invariant: 1. low < high
81+
// 2. mid != high and thus A[mid] != A[high] (no duplicate exists)
82+
// 3. minimum is between [low, high]
83+
// The proof that the loop will exit: after each iteration either the 'high' decreases
84+
// or the 'low' increases, so the interval [low, high] will always shrink.
85+
while (low < high) {
86+
auto mid = low + (high - low) / 2;
87+
if (num[mid] < num[high])
88+
// the mininum is in the left part
89+
high = mid;
90+
else if (num[mid] > num[high])
91+
// the mininum is in the right part
92+
low = mid + 1;
93+
}
94+
95+
return num[low];
96+
}
97+
};
98+
```
99+
100+
### Complexity Analysis
101+
102+
- **Time complexity**: O(log n)
103+
- **Space complexity**: O(1)
104+
105+
### Conclusion
106+
107+
This algorithm effectively finds the minimum element in a rotated sorted array by using binary search. It maintains the loop invariant that the minimum is between the `low` and `high` indices, and ensures the interval `[low, high]` shrinks in each iteration. This solution is efficient and provides a clear way to handle such problems.

0 commit comments

Comments
 (0)