Approach:
- First got the max num from array.
- Using the above max num, got the second max num
- If the 2nd Largest num is present, I have returned it.
- If such 2nd largest num is absent, I have returned -1.
int Solution::solve(vector<int> &A) {
int max_num = INT_MIN;
int max_sec = INT_MIN;
int n = A.size();
for(int i = 0; i < n; i++)
{
max_num = max(max_num, A[i]);
}
for(int i = 0; i < n; i++)
{
if(A[i] != max_num)
{
max_sec = max(max_sec, A[i]);
}
}
if(max_sec != INT_MIN)
{
return max_sec;
}
else{
return -1;
}
}
Top comments (0)