0% found this document useful (0 votes)
9 views2 pages

LG 3

The document presents a C program that uses the divide and conquer technique to find the minimum and maximum numbers in an array. It defines a structure to hold the results and implements a recursive function to compute the min and max values. The program prompts the user for input and displays the results after processing the array.

Uploaded by

astrophileion369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

LG 3

The document presents a C program that uses the divide and conquer technique to find the minimum and maximum numbers in an array. It defines a structure to hold the results and implements a recursive function to compute the min and max values. The program prompts the user for input and displays the results after processing the array.

Uploaded by

astrophileion369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

EX.

11: MAXIMUM AND MINIMUM NUMBERS USING DIVIDE AND


CONQUER TECHNIQUE.

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

You might also like