Skip to main content

Write A program to find the maximum and minimum number using Divide and Conquer method .

Free Discussion

#include<stdio.h> #include<conio.h> int max, min; int a[100]; void maxmin(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; maxmin(i, mid); max1 = max; min1 = min; maxmin(mid+1,j); if(max <max1) max = max1; if(min > min1) min = min1; } } } void main () { int i, num; clrscr(); printf ("\n\t\t\tMAXIMUM & MINIMUM\n\n"); printf ("\nEnter the total number of numbers : "); scanf ("%d",&num); printf ("Enter the numbers : "); for (i=1;i<=num;i++) { scanf ("%d",&a[i]); } max = a[0]; min = a[0]; maxmin(1, num); printf ("Maximum element in an array : %d\n", max); printf ("Minimum element in an array : %d\n", min); getch(); }

OutPut: MAXIMUM &MINIMUM
Enter the total number of numbers : 5
Enter the numbers:34 56 23 74 68
Maximum element in an array : 74
Minimum element in an array : 23

Quote: “I’m selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can’t handle me at my worst, then you sure as hell don’t deserve me at my best.” -Marilyn Monroe