// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the largest
// element in the array arr[]
vector<int> findLargest(int beg, int end,
vector<int> arr, int n)
{
// Base Condition
if (beg == end)
{
// Initialize an empty list
vector<int> compared(n, 0);
compared[0] = 1;
compared[1] = arr[beg];
return compared;
}
// Divide the array into two equal
// length subarrays and recursively
// find the largest among the two
vector<int> compared1 = findLargest(
beg, (beg + end) / 2,
arr, n);
vector<int> compared2 = findLargest(
(beg + end) / 2 + 1,
end, arr, n);
if (compared1[1] > compared2[1])
{
int k = compared1[0] + 1;
// Store length of compared1[]
// in the first index
compared1[0] = k;
// Store the maximum element
compared1[k] = compared2[1];
// Return compared1 which
// contains the maximum element
return compared1;
}
else
{
int k = compared2[0] + 1;
// Store length of compared2[]
// in the first index
compared2[0] = k;
// Store the maximum element
compared2[k] = compared1[1];
// Return compared2[] which
// contains the maximum element
return compared2;
}
}
// Function to print the second largest
// element in the array arr[]
void findSecondLargest(int end, vector<int> arr)
{
// Find the largest element in arr[]
vector<int> compared1 = findLargest(
0, end - 1, arr, end);
// Find the second largest element
// in arr[]
vector<int> compared2 = findLargest(
2, compared1[0] + 2,
compared1,
compared1[0]);
// Print the second largest element
cout << compared2[1];
}
// Driver code
int main()
{
int N = 10;
vector<int> arr{ 20, 1990, 12, 1110, 1,
59, 12, 15, 120, 1110};
findSecondLargest(N, arr);
return 0;
}
// This code is contributed by divyeshrabadiya07