-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax_Min_In_Array.cpp
More file actions
95 lines (89 loc) · 2.01 KB
/
Max_Min_In_Array.cpp
File metadata and controls
95 lines (89 loc) · 2.01 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
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
#include <bits/stdc++.h>
using namespace std;
void printArray(int arr[], int n)
{
cout << "ARRAY IS : ";
int i = 0;
while (i < n)
{
cout << arr[i++] << " ";
}
}
// USING ITERATION
void Max_Min_In_Array(int arr[], int n)
{
int largest = INT_MIN;
int smallest = INT_MAX;
for (int i = 0; i < n; i++)
{
if (arr[i] > largest)
largest = arr[i];
if (arr[i] < smallest)
smallest = arr[i];
}
cout << endl
<< "MAXIMUM DATA IS : " << largest;
cout << endl
<< "MINIMUN DATA IS : " << smallest << endl;
}
// USING RECURSION
struct min_max
{
int smallest;
int largest;
};
struct min_max
Max_Min_In_Array(int arr[], int start, int end)
{
struct min_max temp, temp1, temp2;
if (start == end)
{
temp.largest = arr[start];
temp.smallest = arr[end];
return temp;
}
if (end == (start + 1))
{
if (arr[start] > arr[end])
{
temp.largest = arr[start];
temp.smallest = arr[end];
return temp;
}
else
{
temp.largest = arr[end];
temp.smallest = arr[start];
return temp;
}
}
int mid = (start + end) / 2;
temp1 = Max_Min_In_Array(arr, start, mid);
temp2 = Max_Min_In_Array(arr, mid + 1, end);
if (temp1.largest > temp2.largest)
temp.largest = temp1.largest;
else
temp.largest = temp2.largest;
if (temp1.smallest < temp2.smallest)
temp.smallest = temp1.smallest;
else
temp.smallest = temp2.smallest;
return temp;
}
int main()
{
int n, i = 0;
cout << "Enter the number of Elements : ";
cin >> n;
int arr[n];
while (i < n)
{
cin >> arr[i++];
}
printArray(arr, n);
Max_Min_In_Array(arr, n);
cout << endl
<< "MAXIMUM DATA IS : " << Max_Min_In_Array(arr,0,n-1).largest;
cout << endl
<< "MINIMUN DATA IS : " << Max_Min_In_Array(arr,0,n-1).smallest << endl;
}