Skip to content

Commit

Permalink
status:solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Sundar2k4 committed Aug 3, 2024
0 parents commit 5c35f81
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions secondlargest.c++
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

0 comments on commit 5c35f81

Please sign in to comment.