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:

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


Example 2:

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

Your Task:
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).


1 ≤ N ≤ 106


We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Find second largest element

Output Window