Given an array** arr[] **consisting of** N **integers, the task is to find the second largest element in the given array using **N+log _{2}(N) – 2** comparisons.

**Examples:**

Input: arr[] = {22, 33, 14, 55, 100, 12}Output: 55

Input: arr[] = {35, 23, 12, 35, 19, 100}Output: 35

**Sorting and Two-Traversal Approach:** Refer to Find Second largest element in an array for the solutions using Sorting as well as traversing the array twice.

**Efficient Approach: **

Follow the steps below to solve the problem:

- Find
**the largest**element from the given array and keep track of all the elements**compared**with the largest element.- Split the current array into two equal length subarrays.
- For each subarray,
**recursively**find the**largest element**, and return an array in which the**first index**contains the length of this array,**second index**element contains the**largest element**and the remaining array contains the elements with which the largest element has been compared. - Now, from the two arrays returned by both the subarrays, compare the largest element of both the subarrays and return the array that contains the largest of the two.

- The final array returned by the
**findLargest()**contains its size in the first index, the largest element of the array at the second index and the elements compared with the largest element in the remaining indices. Repeat the above steps using the**findLargest()**to find the**second largest**element of the array from the list of compared elements.

Analysis Of Algorithm:

It is clearly visible from the algorithm that the time complexity of thefindLargest()algorithm isO(N)[N: size of the array]

Hence,(N-1)comparisonsare performed.

Now, the size of the array returned byfindLargest()islogout of which_{2}(N) + 2,logare the ones with which the_{2}(N) elementslargest elementis compared.

Hence, to find the second largest element, the largest among theselogis calculated using_{2}(N) elementslogcomparisons._{2}(N) – 1Therefore,

the total number of comparisons= N – 1 + log_{2}(N) – 1 =N + log_{2}(N) – 2

Below is the implementation of the above approach:

## C++

`// 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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to implement ` `# the above approach ` ` ` `# Function to find the largest ` `# element in the array arr[] ` `def` `findLargest(beg, end, arr, n): ` ` ` ` ` `# Base Condition ` ` ` `if` `(beg ` `=` `=` `end): ` ` ` ` ` `# Initialize an empty list ` ` ` `compared ` `=` `[` `0` `]` `*` `n ` ` ` `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 ` ` ` `compared1 ` `=` `findLargest(beg, (beg` `+` `end)` `/` `/` `2` `, ` ` ` `arr, n) ` ` ` ` ` `compared2 ` `=` `findLargest((beg` `+` `end)` `/` `/` `2` `+` `1` `, end, ` ` ` `arr, n) ` ` ` ` ` `if` `(compared1[` `1` `] > compared2[` `1` `]): ` ` ` `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` `: ` ` ` `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[] ` `def` `findSecondLargest(end, arr): ` ` ` ` ` `# Find the largest element in arr[] ` ` ` `compared1 ` `=` `findLargest(` `0` `, end` `-` `1` `, arr, end) ` ` ` ` ` `# Find the second largest element ` ` ` `# in arr[] ` ` ` `compared2 ` `=` `findLargest(` `2` `, compared1[` `0` `]` `+` `2` `, ` ` ` `compared1, ` ` ` `compared1[` `0` `]) ` ` ` ` ` `# Print the second largest element ` ` ` `print` `(compared2[` `1` `]) ` ` ` ` ` `# Driver Code ` `N ` `=` `10` ` ` `arr ` `=` `[` `20` `, ` `1990` `, ` `12` `, ` `1110` `, ` `1` `, ` `59` `, ` `12` `, ` `15` `, ` `120` `, ` `1110` `] ` ` ` `findSecondLargest(N, arr) ` |

*chevron_right*

*filter_none*

**Output:**

1110

**Time Complexity: **O(N)

**Auxiliary Space:** O(logN)

## Recommended Posts:

- Find a pair in Array with second largest product
- Find extra element in the second array
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count distinct elements after adding each element of First Array with Second Array
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Find numbers which are multiples of first array and factors of second array
- Sum of all second largest divisors after splitting a number into one or more parts
- Program to find largest element in an array
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- K'th Smallest/Largest Element in Unsorted Array | Set 1
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
- Number of ways to change the Array such that largest element is LCM of array
- Find the smallest and second smallest elements in an array
- Find the first, second and third minimum elements in an array
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Find the sum of the first half and second half elements of an array
- Find prime numbers in the first half and second half of an array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.