-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondLargest.cpp
More file actions
37 lines (32 loc) · 850 Bytes
/
SecondLargest.cpp
File metadata and controls
37 lines (32 loc) · 850 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
// Take input a stream of n integer elements, find the second largest element present.
// The given elements can contain duplicate elements as well. If only 0 or 1 element is given, the second largest should be INT_MIN ( - 2^31 ).
// Input format :
// Line 1 : Total number of elements (n)
// Line 2 : N elements (separated by space)
// Sample Input 1:
// 4
// 3 9 0 9
// Sample Output 1:
// 3
#include<iostream>
using namespace std;
#include <climits>
int main() {
int n;
cin >> n;
int max = INT_MIN, secondMax = INT_MIN;
int num;
int count = 1;
while(count <= n) {
cin >> num;
if(num > max) {
secondMax = max;
max = num;
}
else if(num > secondMax && num != max) {
secondMax = num;
}
count++;
}
cout << secondMax << endl;
}