Find second largest element
Basic Accuracy: 44.66% Submissions: 16905 Points: 1

Given an array of elements. Your task is to find the second maximum element in the array.

Example 1:

Input:
N=5
arr[] = { 2, 4, 5, 6, 7 }
Output:  6
Explanation:
The largest element is 7 and
the second largest element is 6.


Example 2:

Input:
N=6
arr[] = { 7, 8, 2, 1, 4, 3 }
Output:  7


Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function print2largest() that takes array A and integer N as parameters and returns the second maximum element in the array. If there does not exist any second largest element, then return -1.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ N ≤ 106

We are replacing the old Disqus forum with the new Discussions section given below.