Question Name:Minimum and Maximum

#include <iostream>
using namespace std;
int main()
{
int a[10],b,i,max=0,min=100;
  
  cin>>b;
  for(i=0;i<b;i++) 
  { 
    cin>>a[i];
  }
   for(i=0;i<b;i++) 
  { if(min>a[i]) { min=a[i]; } 
   if(max<a[i]) { max=a[i]; } 
  } 
  cout<<"Minimum element is "<<min<<endl;
  cout<<"Maximum element is "<<max;
	return 0;
}
  • Problem Description
    Divide the problem into a number of subproblems

    There must be base case (to stop recursion).

    Conquer (solve) each subproblem recursively
    Combine (merge) solutions to subproblems into a
    solution to the original problem 

    Nontrivial strategy:
    1. Split the array in half
    2. Find the MAX and MIN of both halves
    3. Compare the 2 MAXes and compare the 2 MINs to get
    overall MAX and MIN.
  • Test Case 1
    Input (stdin)4
    2 4 1 10
    Expected OutputMinimum element is 1
    Maximum element is 10
  • Test Case 2
    Input (stdin)3
    12 10 2
    Expected OutputMinimum element is 2
    Maximum element is 12
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock