Skip to main content

Divide and conquer min max

        Divide and conquer min max

 #include<stdio.h>

#include<stdio.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[i];

    min = a[j];

   }

   else

   {

    max = a[j];

    min = a[i];

   }

  }

  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;

  }

 }

}

int main ()

{

 int i, num;

 printf ("\nEnter the total number of numbers : ");

 scanf ("%d",&num);

 printf ("Enter the numbers : \n");

 for (i=1;i<=num;i++)

  scanf ("%d",&a[i]);


 max = a[0];

 min = a[0];

 maxmin(1, num);

 printf ("Minimum element in an array : %d\n", min);

 printf ("Maximum element in an array : %d\n", max);

 return 0;

}


Comments

Popular posts from this blog

C program to count total number of notes in given amount

 #include <iostream> using namespace std; int main() {     // 500,100,50,20,10,5,2,1     int n;     int count1,count2,count3,count4,count5,count6,count7,count8;     count1=count2=count3=count4=count5=count6=count7=count8=0;     cin>>n;         if(n>=500){           count1=n/500;           n=n%500;         }         if(n>=100){             count2=n/100;             n=n%100;         }         if(n>=50){             count3=n/50;       ...

"How to Choose the Perfect Earphones for Your Needs"

 "How to Choose the Perfect Earphones for    Your Needs" Earphones are an essential accessory for many people, whether it's for listening to music, watching videos, or making phone calls. With so many different options available, it can be challenging to choose the right pair of earphones for your needs. In this blog post, we'll discuss some factors to consider when selecting the perfect earphones for you. Sound Quality Sound quality is the most important factor to consider when choosing earphones. The quality of sound depends on the type of earphone you choose. In-ear earphones provide better sound quality and noise isolation than on-ear earphones. You should also look for earphones with a wide frequency response range for a better listening experience. Comfort Comfort is also a crucial factor when selecting earphones. Choose earphones with soft ear tips that fit your ears well to avoid discomfort during extended use. In-ear earphones with multiple ear ...