-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1array2ndLargest.cpp
More file actions
33 lines (33 loc) · 924 Bytes
/
1array2ndLargest.cpp
File metadata and controls
33 lines (33 loc) · 924 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
#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 largest = arr[0];
int second_largest = INT_MIN;
int smallest = arr[0];
int second_smallest = INT_MAX;
for(int i=1;i<n;i++){
if(arr[i]>largest){
second_largest=largest; // very important line as it updates the second largest to the previous largest
largest = arr[i];
}
else if(arr[i]>second_largest&&arr[i]!=largest){
second_largest=arr[i];
}
if(arr[i]<smallest){
second_smallest = smallest;
smallest=arr[i];
}
else if(arr[i]<second_smallest&&arr[i]!=smallest){
second_smallest=arr[i];
}
}
cout<<"second largest no. is "<<second_largest<<endl;
cout<<"second smallest no. is "<<second_smallest;
return 0;
}