File tree 1 file changed +51
-0
lines changed
Difficulty: Easy/Second Largest
1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ // { Driver Code Starts
2
+ #include < bits/stdc++.h>
3
+
4
+ using namespace std ;
5
+
6
+
7
+ // } Driver Code Ends
8
+ // User function template for C++
9
+ class Solution {
10
+ public:
11
+ // Function returns the second
12
+ // largest elements
13
+ int getSecondLargest (vector<int > &arr) {
14
+ // Code Here
15
+ sort (arr.begin (),arr.end ());
16
+ int lar=arr[arr.size ()-1 ],smax=-1 ;
17
+ for (int i=arr.size ()-2 ;i>=0 ;i--){
18
+ if (arr[i]!=lar){
19
+ smax=arr[i];
20
+ break ;
21
+ }
22
+ }
23
+ return smax;
24
+
25
+ }
26
+ };
27
+
28
+ // { Driver Code Starts.
29
+
30
+ int main () {
31
+ int t;
32
+ cin >> t;
33
+ cin.ignore ();
34
+ while (t--) {
35
+ vector<int > arr;
36
+ string input;
37
+ getline (cin, input);
38
+ stringstream ss (input);
39
+ int number;
40
+ while (ss >> number) {
41
+ arr.push_back (number);
42
+ }
43
+ Solution ob;
44
+ int ans = ob.getSecondLargest (arr);
45
+ cout << ans << endl;
46
+ cout << " ~" << endl;
47
+ }
48
+ return 0 ;
49
+ }
50
+
51
+ // } Driver Code Ends
You can’t perform that action at this time.
0 commit comments