forked from Kuwarsaab/git-learn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum.cpp
More file actions
36 lines (30 loc) · 741 Bytes
/
minimum.cpp
File metadata and controls
36 lines (30 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;
int findMin(vector<int>& arr) {
int low = 0, high = arr.size() - 1;
int ans = INT_MAX;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[low] <= arr[high]) {
ans = min(ans, arr[low]);
break;
}
if (arr[low] <= arr[mid]) {
ans = min(ans, arr[low]);
low = mid + 1;
}
else {
ans = min(ans, arr[mid]);
high = mid - 1;
}
}
return ans;
}
int main() {
vector<int> arr = {4, 5, 6, 7, 0, 1, 2};
cout << "The minimum element is: " << findMin(arr) << endl;
return 0;
}