-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHard_Prob235.cpp
More file actions
61 lines (49 loc) · 1.49 KB
/
Hard_Prob235.cpp
File metadata and controls
61 lines (49 loc) · 1.49 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Given an array of numbers of length N, find both the minimum and maximum using less than 2 * (N - 2) comparisons.*/
#include <iostream>
#include <vector>
#include <cmath>
#include<bits/stdc++.h>
using namespace std;
template <typename T>
struct MinMax {
T min;
T max;
};
template <typename T>
MinMax<T> findMinMax(vector<T> array) {
int size = array.size();
MinMax<T> result ;
if (size < 1) {
result ;
} else if (size == 1) {
result.min = array[0];
result.max = array[0];
return result;
} else {
MinMax<T> rightResult, leftResult;
typename vector<T>::const_iterator beginIt = array.begin();
typename vector<T>::const_iterator middleIt = array.begin() + int(size/2);
typename vector<T>::const_iterator endIt = array.end();
vector<T> rightArray(beginIt, middleIt);
vector<T> leftArray(middleIt, endIt);
rightResult = findMinMax(rightArray);
leftResult = findMinMax(leftArray);
if (rightResult.min > leftResult.min) {
result.min = leftResult.min;
} else {
result.min = rightResult.min;
}
if (rightResult.max > leftResult.max) {
result.max = rightResult.max;
} else {
result.max = leftResult.max;
}
}
return result;
};
int main(int argc, char *argv[])
{
vector<int> array = {5, 6, -1, 4};
MinMax result = findMinMax(array);
cout << result.min << " " << result.max << endl;
}