-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA_7.cpp
More file actions
38 lines (31 loc) · 771 Bytes
/
DSA_7.cpp
File metadata and controls
38 lines (31 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Author - Sandeep Das
1.) Q2 of 450DSA
2.) Problem - Print the max. and min. element(s) in an array
3.) Input : 1, 2, 3, 4, 5
Output : 1 5
4.) T.C. = O(n)
5.) Algo :- Take 2 variables max and min. Initialize them as
max=min=a[0]. Iterate through the array and use if-else clause
if(ele[i]>max) => change max as max = ele[i]
else-if(ele[i]<min) => change min as min = ele[i]
*/
#include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int min,max;
min=max=arr[0];
for(int i=1;i<n;i++){
if(arr[i]>max)
max = arr[i];
else if(arr[i]<min)
min = arr[i];
}
cout<<min<<" "<<max;
return 0;
}