MaxMin
Maxmin
#include <stdio.h>
int a[100];
int max, min;
void max_min(int i, int j) {
int max1, min1, 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;
max_min(i, mid);
max1 = max;
min1 = min;
max_min(mid + 1, j);
if (max < max1)
max = max1;
if (min > min1)
min = min1;
}
}
}
int main() {
int n, i;
printf("Enter the total number of elements in the array: ");
scanf("%d", &n);
printf("Enter the array elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
max = a[0];
min = a[0];
max_min(0, n-1);
printf("Minimum array: %d\n", min);
printf("Maximum array: %d\n", max);
return 0;
}
Comments
Post a Comment