LG 3
LG 3
PROGRAM:
#include <stdio.h>
#include <conio.h>
struct Result {
int min;
int max;
};
struct Result findMinMax(int arr[], int low, int high) {
struct Result result, leftResult, rightResult;
int mid;
if (low == high) {
result.min = result.max = arr[low];
return result;
}
if (high == low + 1) {
if (arr[low] < arr[high]) {
result.min = arr[low];
result.max = arr[high];
} else {
result.min = arr[high];
result.max = arr[low];
}
return result;
}mid = (low + high) / 2;
leftResult = findMinMax(arr, low, mid);
rightResult = findMinMax(arr, mid + 1, high);
result.min = (leftResult.min < rightResult.min) ? leftResult.min : rightResult.min;
result.max = (leftResult.max > rightResult.max) ? leftResult.max : rightResult.max;
return result;
}void main() {
int arr[100], n, i;
struct Result finalResult;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
finalResult = findMinMax(arr, 0, n - 1);
printf("\nMinimum element: %d", finalResult.min);
printf("\nMaximum element: %d", finalResult.max);
getch();
}
OUTPUT:
Enter number of elements: 6
Enter 6 elements:
23 15 89 4 67 35
Minimum element: 4
Maximum element: 89