Find Second largest element in an array | Set 2
Given an array arr[] consisting of N integers, the task is to find the second largest element in the given array using N+log2(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 the findLargest() algorithm is O(N) [N: size of the array]
Hence, (N-1) comparisons are performed.
Now, the size of the array returned by findLargest() is log2(N) + 2, out of which log2(N) elements are the ones with which the largest element is compared.
Hence, to find the second largest element, the largest among these log2(N) elements is calculated using log2(N) – 1 comparisons.
Therefore, the total number of comparisons = N – 1 + log2(N) – 1 = N + log2(N) – 2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > findLargest( int beg, int end,
vector< int > arr, int n)
{
if (beg == end)
{
vector< int > compared(n, 0);
compared[0] = 1;
compared[1] = arr[beg];
return compared;
}
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;
compared1[0] = k;
compared1[k] = compared2[1];
return compared1;
}
else
{
int k = compared2[0] + 1;
compared2[0] = k;
compared2[k] = compared1[1];
return compared2;
}
}
void findSecondLargest( int end, vector< int > arr)
{
vector< int > compared1 = findLargest(
0, end - 1, arr, end);
vector< int > compared2 = findLargest(
2, compared1[0] + 2,
compared1,
compared1[0]);
cout << compared2[1];
}
int main()
{
int N = 10;
vector< int > arr{ 20, 1990, 12, 1110, 1,
59, 12, 15, 120, 1110};
findSecondLargest(N, arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int [] findLargest( int beg, int end,
int []arr, int n)
{
if (beg == end)
{
int []compared = new int [n];
compared[ 0 ] = 1 ;
compared[ 1 ] = arr[beg];
return compared;
}
int []compared1 = findLargest(beg,
(beg + end) /
2 , arr, n);
int []compared2 = findLargest((beg + end) /
2 + 1 ,
end, arr, n);
if (compared1[ 1 ] > compared2[ 1 ])
{
int k = compared1[ 0 ] + 1 ;
compared1[ 0 ] = k;
compared1[k] = compared2[ 1 ];
return compared1;
}
else
{
int k = compared2[ 0 ] + 1 ;
compared2[ 0 ] = k;
compared2[k] = compared1[ 1 ];
return compared2;
}
}
static void findSecondLargest( int end,
int []arr)
{
int []compared1 = findLargest( 0 , end - 1 ,
arr, end);
int []compared2 = findLargest( 2 , compared1[ 0 ] + 2 ,
compared1,
compared1[ 0 ]);
System.out.print(compared2[ 1 ]);
}
public static void main(String[] args)
{
int N = 10 ;
int []arr ={ 20 , 1990 , 12 , 1110 , 1 ,
59 , 12 , 15 , 120 , 1110 };
findSecondLargest(N, arr);
}
}
|
Python3
def findLargest(beg, end, arr, n):
if (beg = = end):
compared = [ 0 ] * n
compared[ 0 ] = 1
compared[ 1 ] = arr[beg]
return compared
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
compared1[ 0 ] = k
compared1[k] = compared2[ 1 ]
return compared1
else :
k = compared2[ 0 ] + 1
compared2[ 0 ] = k
compared2[k] = compared1[ 1 ]
return compared2
def findSecondLargest(end, arr):
compared1 = findLargest( 0 , end - 1 , arr, end)
compared2 = findLargest( 2 , compared1[ 0 ] + 2 ,
compared1,
compared1[ 0 ])
print (compared2[ 1 ])
N = 10
arr = [ 20 , 1990 , 12 , 1110 , 1 , 59 , 12 , 15 , 120 , 1110 ]
findSecondLargest(N, arr)
|
C#
using System;
class GFG{
static int [] findLargest( int beg, int end,
int []arr, int n)
{
if (beg == end)
{
int []compared = new int [n];
compared[0] = 1;
compared[1] = arr[beg];
return compared;
}
int []compared1 = findLargest(beg,
(beg + end) /
2, arr, n);
int []compared2 = findLargest((beg + end) /
2 + 1,
end, arr, n);
if (compared1[1] > compared2[1])
{
int k = compared1[0] + 1;
compared1[0] = k;
compared1[k] = compared2[1];
return compared1;
}
else
{
int k = compared2[0] + 1;
compared2[0] = k;
compared2[k] = compared1[1];
return compared2;
}
}
static void findSecondLargest( int end,
int []arr)
{
int []compared1 = findLargest(0, end - 1,
arr, end);
int []compared2 = findLargest(2, compared1[0] + 2,
compared1,
compared1[0]);
Console.WriteLine(compared2[1]);
}
static public void Main ()
{
int N = 10;
int []arr = { 20, 1990, 12, 1110, 1,
59, 12, 15, 120, 1110 };
findSecondLargest(N, arr);
}
}
|
Javascript
<script>
function findLargest(beg,end,arr,n)
{
if (beg == end)
{
let compared = new Array(n);
for (let i=0;i<n;i++)
compared[i]=0;
compared[0] = 1;
compared[1] = arr[beg];
return compared;
}
let compared1 = findLargest(beg,
Math.floor((beg + end) /
2), arr, n);
let compared2 = findLargest(Math.floor((beg + end) /
2 )+ 1,
end, arr, n);
if (compared1[1] > compared2[1])
{
let k = compared1[0] + 1;
compared1[0] = k;
compared1[k] = compared2[1];
return compared1;
}
else
{
let k = compared2[0] + 1;
compared2[0] = k;
compared2[k] = compared1[1];
return compared2;
}
}
function findSecondLargest(end,arr)
{
let compared1 = findLargest(0, end - 1,
arr, end);
let compared2 = findLargest(2, compared1[0] + 2,
compared1,
compared1[0]);
document.write(compared2[1]);
}
let N = 10;
let arr=[20, 1990, 12, 1110, 1,
59, 12, 15, 120, 1110];
findSecondLargest(N, arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(logN)
Last Updated :
14 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...