forked from dharmanshu1921/Daa-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.cpp
68 lines (51 loc) · 1.33 KB
/
code.cpp
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
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
void findMinAndMax(vector<int> const &nums, int low, int high, int &min, int &max)
{
if (low == high)
{
if (max < nums[low]) {
max = nums[low];
}
if (min > nums[high]) {
min = nums[high];
}
return;
}
if (high - low == 1)
{
if (nums[low] < nums[high])
{
if (min > nums[low]) {
min = nums[low];
}
if (max < nums[high]) {
max = nums[high];
}
}
else {
if (min > nums[high]) {
min = nums[high];
}
if (max < nums[low]) {
max = nums[low];
}
}
return;
}
int mid = (low + high) / 2;
findMinAndMax(nums, low, mid, min, max);
findMinAndMax(nums, mid + 1, high, min, max);
}
int main()
{
vector<int> nums = { 7, 2, 9, 3, 1, 6, 7, 8, 4 };
int max = INT_MIN, min = INT_MAX;
int n = nums.size();
findMinAndMax(nums, 0, n - 1, min, max);
cout << "The minimum array element is " << min << endl;
cout << "The maximum array element is " << max << endl;
return 0;
}