Skip to content

Commit 734a20f

Browse files
committed
min ele in rotated array
1 parent 2242fbf commit 734a20f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Searching/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
|2| [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/)|[Solution](single_element_in_sorted_array.cpp)|
3030
|3| [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)|Pending|
3131
|4| [Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)|Pending|
32-
|5| [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)|Pending|
32+
|5| [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)|[Solution](min_in_rotated_arr.cpp)|
3333
|6| [Find Peak Element](https://leetcode.com/problems/find-peak-element/)|[Solution](peak_ele.cpp)|
3434
|7| [Find Right Interval](https://leetcode.com/problems/find-right-interval/)|Pending|
3535
|8| [Reach a Number](https://leetcode.com/problems/reach-a-number/)|Pending|

Searching/min_in_rotated_arr.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int findMin(vector<int> &num)
5+
{
6+
if (num.empty())
7+
return 0;
8+
int low = 0;
9+
int high = num.size() - 1;
10+
int mid;
11+
12+
while (low < high)
13+
{
14+
mid = (low + high) / 2;
15+
if (num[low] > num[mid])
16+
{
17+
low++;
18+
high = mid;
19+
}
20+
else if (num[mid] > num[high])
21+
{
22+
low = ++mid;
23+
}
24+
else
25+
high = mid;
26+
}
27+
28+
return num[low];
29+
}
30+
31+
int main()
32+
{
33+
int n, target;
34+
cin >> n;
35+
vector<int> v;
36+
for (int i = 0; i < n; i++)
37+
{
38+
int temp;
39+
cin >> temp;
40+
v.push_back(temp);
41+
}
42+
cout << findMin(v) << endl;
43+
return 0;
44+
}

0 commit comments

Comments
 (0)