forked from im-anahata/-HacktoberFest2k22-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_min.c
56 lines (56 loc) · 1.07 KB
/
max_min.c
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
#include <stdio.h>
int max, min;
int a[10];
void maxMin(int i, int j)
{
int maxTemp, minTemp, mid;
if (i == j)
{
max = min = a[i];
}
else
{
if (i == j - 1)
{
if (a[i] < a[j])
{
max = a[j];
min = a[i];
}
else
{
max = a[i];
min = a[j];
}
}
else
{
mid = (i + j) / 2;
maxMin(i, mid);
maxTemp = max;
minTemp = min;
maxMin(mid + 1, j);
if (max < maxTemp)
max = maxTemp;
if (min > minTemp)
min = minTemp;
}
}
}
int main()
{
int i, n;
printf("\nEnter size of the array : ");
scanf("%d", &n);
printf("Enter the elements of the array : ");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
max = a[0];
min = a[0];
maxMin(0, n-1);
printf("Max element : %d\n", max);
printf("Min element : %d\n", min);
return 0;
}