-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinMax.cpp
More file actions
47 lines (43 loc) · 971 Bytes
/
MinMax.cpp
File metadata and controls
47 lines (43 loc) · 971 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
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<int> minmax(int *arr, int low, int high);
int main() {
ifstream fin("MinMax.in");
ofstream fout("MinMax.out");
vector<int> result(2, 0);
int n, *arr;
fin >> n;
arr = new int[n];
for(int i=0; i<n; i++)
fin >> arr[i];
result = minmax(arr, 0, n-1);
fout << "The min is: " << result[0] << endl;
fout << "The max is: " << result[1] << endl;
delete[] arr;
return 0;
}
vector<int> minmax(int *arr, int low, int high) {
vector<int> res(2, 0);
if((high-low)==1) {
if(arr[low]<arr[high]) {
res[0] = arr[low];
res[1] = arr[high];
return res;
}
else {
res[0] = arr[high];
res[1] = arr[low];
return res;
}
}
else {
int mid = (low+high)/2;
vector<int> res1 = minmax(arr, low, mid);
vector<int> res2 = minmax(arr, mid+1, high);
res[0] = res1[0] < res2[0] ? res1[0] : res2[0];
res[1] = res1[1] > res2[1] ? res1[1] : res2[1];
return res;
}
}