-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_min_array_DSA_Problem_2_Array.cpp
161 lines (143 loc) · 3.19 KB
/
max_min_array_DSA_Problem_2_Array.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
using namespace std;
//Pair Structure
struct Pair
{
int max;
int min;
};
// Simple Linear Search Method
Pair max_min_linear_search(int arr[], int size)
{
struct Pair result;
if (arr[0] > arr[1])
{
result.max = arr[0];
result.min = arr[1];
}
else
{
result.max = arr[1];
result.min = arr[0];
}
for (int i = 2; i < size; i++)
{
if (arr[i] > result.max)
{
result.max = arr[i];
}
if (arr[i] < result.min)
{
result.min = arr[i];
}
}
return result;
}
//Tournament Approch
Pair max_min_tournament(int arr[], int low, int high)
{
struct Pair result, mml, mmr;
int mid;
//if size = 1 then both max and min will be same number that is index 1 element
if (low == high)
{
result.max = arr[0];
result.min = arr[0];
return result;
}
//if size = 2 then there will be comparision between first and second index element
if (high == low + 1)
{
if (arr[low] > arr[high])
{
result.max = arr[low];
result.min = arr[high];
}
else
{
result.max = arr[high];
result.min = arr[low];
}
return result;
}
//Splitting task in two halfs and operating function
mid = (low + high) / 2;
mml = max_min_tournament(arr, low, mid);
mmr = max_min_tournament(arr, mid + 1, high);
// Compare between max and min elements of both the halfs
if (mml.min < mmr.min)
result.min = mml.min;
else
result.min = mmr.min;
if (mml.max > mmr.max)
result.max = mml.max;
else
result.max = mmr.max;
return result;
}
//Compare in pairs
struct Pair comp_pair(int arr[], int n)
{
struct Pair minmax;
int i;
//if n is even compare between first and second element
if (n % 2 == 0)
{
if (arr[0] > arr[1])
{
minmax.max = arr[0];
minmax.min = arr[1];
}
else
{
minmax.max = arr[1];
minmax.min = arr[0];
}
i = 2;
}
//if n is odd then max and min is first element it self
else
{
minmax.max = arr[0];
minmax.min = arr[0];
i = 1;
}
//Compare between pairs
while (i < n - 1)
{
if (arr[i] > arr[i + 1])
{
if (arr[i] > minmax.max)
{
minmax.max = arr[i];
}
if (arr[i + 1] < minmax.min)
{
minmax.min = arr[i + 1];
}
}
else
{
if (arr[i + 1] > minmax.max)
{
minmax.max = arr[i + 1];
}
if (arr[i] < minmax.min)
{
minmax.min = arr[i];
}
}
i += 2;
}
return minmax;
}
//Driver program
int main()
{
int arr[] = {2, 1, 0, 67, -99, 8, 5, 7, 8, 90};
int size = sizeof(arr) / sizeof(arr[0]);
struct Pair result = comp_pair(arr, size);
cout << "Max number is :" << result.max << endl;
cout << "Min number is :" << result.min << endl;
return 0;
}