-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5c35f81
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//{ Driver Code Starts | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
// User function template for C++ | ||
class Solution | ||
{ | ||
public: | ||
// Function returns the second | ||
// largest elements | ||
int print2largest(vector<int> &arr) | ||
{ | ||
int n = arr.size(); | ||
if (n < 2) | ||
{ | ||
return -1; | ||
} | ||
else | ||
{ | ||
int large = INT_MIN; | ||
int second_large = INT_MIN; | ||
int i; | ||
for (i = 0; i < n; i++) | ||
{ | ||
if (arr[i] > large) | ||
{ | ||
second_large = large; | ||
large = arr[i]; | ||
} | ||
else if (arr[i] > second_large && arr[i] != large) | ||
{ | ||
second_large = arr[i]; | ||
} | ||
} | ||
return second_large; | ||
} | ||
} | ||
}; | ||
|
||
//{ Driver Code Starts. | ||
|
||
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.print2largest(arr); | ||
cout << ans << endl; | ||
} | ||
return 0; | ||
} | ||
|
||
// } Driver Code Ends |