-
Notifications
You must be signed in to change notification settings - Fork 0
/
Max Item D&C.cpp
50 lines (35 loc) · 982 Bytes
/
Max Item D&C.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
#include <iostream>
#include <climits>
using namespace std;
void findMinAndMax(int arr[], int low, int high, int &max)
{
if (low == high)
{
if (max < arr[low])
max = arr[low];
return;
}
else if (high > low)
{
if (arr[low] < arr[high])
if (max < arr[high])
max = arr[high];
else
if (max < arr[low])
max = arr[low];
int mid = (low + high) / 2;
findMinAndMax(arr, low, mid, max);
findMinAndMax(arr, mid + 1, high, max);
}
}
int main()
{
int arr[] = { 7, 2, 9, 3, 1, 63, 7, 8, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
// initialize the minimum element by infinity and the
// maximum element by minus infinity
int max = INT_MIN;
findMinAndMax(arr, 0, n - 1, max);
cout << "The maximum element in the array is " << max<<endl;
return 0;
}