-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecond Largest.cpp
43 lines (41 loc) · 939 Bytes
/
Second Largest.cpp
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
39
40
41
42
43
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function returns the second
// largest elements
int getSecondLargest(vector<int> &arr) {
int largest = -1;
int secLargest = -1;
for(int i=0;i<arr.size();i++){
if(arr[i]>largest){
secLargest = largest;
largest = arr[i];
}
else if(arr[i]<largest && arr[i]> secLargest){
secLargest = arr[i];
}
}
return secLargest;
}
};
int main(){
int t;
cin >> t;
cin.ignore();
while(t--){
vector<int> arr;
string input;
getline(cin,input);
stringstream ss(input);
int number;
while(ss>>number){
arr.push_back(number);
}
Solution ob;
int ans = ob.getSecondLargest(arr);
cout << ans << endl;
cout << "~" << endl;
}
return 0;
}