Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

G. H. RAISONI COLLEGE OF ENGG.

& MANAGEMENT
Gat No. 1200, Wagholi, Pune – 412 207
(An Autonomous Institute Affiliated to Savitribai Phule Pune University)

Session 2021-22

Presentation on

“Maximum and Minimum finding”


By

B98 SUSHANT MORE


B99 SUSHEEL MARATHE
B100 SUYASH BHADGAONKAR

SY B.Tech: Div B
Solution Idea -

• We declare and initialize variables max and min to store


maximum and minimum.

• By the end of the loop, the minimum and maximum values of the
array will be stored in the variables min and max.

• We traverse the array from i = 1 to n – 1 and compare each


element with min and max.

2
• If (X[i] < min), it means we have found a value smaller than
minimum till ith index. So we update min with X[i] i.e min = x[i.]

• Else if(x[i] > max), it means we have found a value greater than
maximum till ith index. So we update max with X[i] i.e max =
x[i].

3
Solution Pseudocode -

4
• In the worst case, we are making two comparisons at each step of
the iteration. This case will arise if the array is sorted in
descending order. In this situation, the first if statement will be
false every time, and the second if the statement is true every
time. So total no. Of comparisons in the worst case = 2*(n-1) =
2n – 2.
• The best-case occurs when elements are sorted in ascending
order. In this situation, a total of n – 1 comparisons have been
made.

5
Code -
#include<iostream> Int minimum(int a[], int n) Cout << “Enter the array elements : “ << endl;
{ for (int i = 0; i<n; i++)
using namespace std; int min = a[0]; {
for (int i = 0; i < n; i++)
int maximum(int a[], int n) cin >> array[i];
{
if (a[i] < min) }
{
{ cout << “The array elements are : “ << endl;
int max = a[0]; min = a[i]; for (int i = 0; i<n; i++)
for (int i = 0; i < n; i++)
} {
} cout << array[i] << “ “;
{ return min; }
} cout << endl<< “The maximum element in
if (a[i] > max) Int main()
the array is : “ << maximum(array, n);
{ {
int n; cout << endl<< “The maximum element in
max = a[i]; cout << “Enter the size of array : “ << endl; the array is : “ << minimum(array, n);
cin >> n; return 0;
} int array[n]; }
}
return max; 6
Output -

7
Minimum and Maximum element of an Array using STL -

• The Standard Template Library (STL) is a set of C++ template


classes to provide common programming data structures and
functions such as lists, stacks, arrays, etc.

• It is a library of container classes, algorithms, and iterators

8
Approach -

• Min or Minimum element can be found with the help of


*min_element() function provided in STL.

• Max or Maximum element can be found with the help of


*max_element() function provided in STL.

9
Syntax -

10
Code -
#include<bits/stdc++.h> cout << “The input array is : “ << endl;
for (int i = 0; i < n; i++)
using namespace std;
{
cout << arr[i] << “ “;
}
int main() cout << endl <<“Minimum Element = “ <<
*min_element(arr, arr + n);
{
cout << endl << “Maximum Element = “ <<
int n; *max_element(arr, arr + n);
return 0;
cout << “Enter the size of an array : “ << endl; }
cin >> n;
cout << “Enter the array elements : “ << endl;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
11
}
Output -

12
13

You might also like