Professional Documents
Culture Documents
Daa Tae 1
Daa Tae 1
& MANAGEMENT
Gat No. 1200, Wagholi, Pune – 412 207
(An Autonomous Institute Affiliated to Savitribai Phule Pune University)
Session 2021-22
Presentation on
SY B.Tech: Div B
Solution Idea -
• By the end of the loop, the minimum and maximum values of the
array will be stored in the variables 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 -
8
Approach -
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