Third largest element | Practice | GeeksforGeeks

Given an array of distinct elements. Find the third largest element in it. 
Suppose you have A[] = {1, 2, 3, 4, 5, 6, 7}, its output will be 5 because it is the 3 largest element in the array A.
Example 1:
Input:
N = 5
A[] = {2,4,1,3,5}
Output: 3

Example 2:
Input:
N = 2
A[] = {10,2}
Output: -1
Your Task:
Complete the function thirdLargest() which takes the array a[] and the size of the array, n, as input parameters and returns the third largest element in the array. It return -1 if there are less than 3 elements in the given array.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 105
1 ≤ A[i] ≤ 105
-2
nikhilravi562 days ago
O(1) solution
 
class Solution{   int thirdLargest(int a[], int n)   {    // Your code here    if(n<3)        return -1;       Arrays.sort(a);       return a[n-3];   }}
-1
manishamali3 days ago
int thirdLargest(int a[], int n)   {        //Your code here        sort(a,a+n);        return a[n-3];           }
+2
goyalharshit1505 days ago
int thirdLargest(int a[], int n)   {        int m1 = a[0], m2=INT_MIN, m3=INT_MIN;        for(int i=1;i<n;i++){            if(a[i]>m1){                m3= m2;                m2=m1;                m1 = a[i];            }else if(a[i]>m2 && a[i]<m1){                m3 = m2;                m2 = a[i];            }else if(a[i] > m3) {                m3 = a[i];            }        }        return m3;   }
+1
rockachi6 days ago
Code Works for Distinct Elements:-
 
int thirdLargest(int a[], int n)   {               //Your code here         if(n<3)    return -1;        int max1=a[0];    for(int i=1;i<n;i++){        if(a[i]>max1)        max1=a[i];    }        int max2=0;    for(int i=0;i<n;i++){        if((a[i]!=max1)&&(a[i]>max2 ))        max2=a[i];    }   int max3=0;  for(int i=0;i<n;i++){       if((a[i]!=max1)&&(a[i]!=max2 )&&(a[i]>max3))       max3=a[i];  }   return max3;   } 
Code Works For Duplicate Elements also:-
    
 
   int thirdLargest(int a[], int n)   {              int temp=0;      for(int j=0;j<n-2;j++)      {          for(int i=0;i<n-1;i++)      {          if(a[i]>a[i+1])          {              temp=a[i];              a[i] = a[i+1];              a[i+1]=temp;          }      }           }      return a[n-3];  }
 
0
theayushsrivastava1 week ago
Java Solution Time Complexity: O(n)
 
 
 
int thirdLargest(int a[], int n)   {    // Your code here    if(n<3){        return -1;    }        int f=a[0];    int s=Integer.MIN_VALUE;    int t=Integer.MIN_VALUE;        for(int i=1;i<n;i++){                if(a[i]>f){                        t=s;            s=f;            f=a[i];                   }                else if(a[i]>s && a[i]<f){                        t=s;            s=a[i];                                }                else if(a[i]>t){            t=a[i];        }                    }        return t;           }
0
thecipher1 week ago
0
thecipher1 week ago
int thirdLargest(int a[], int n)   {        sort(a,a+n);        for(int i=0;i<n;i++)        {            return a[n-3];        }        return -1;   }
but i need the solution in O(n)
+1
shubhisharma1 week ago
JAVA :
 
 static int thirdLargest(int a[], int n)   {    // Your code here        if(n<3)    {        return -1;    }    else    {      sort(a,n);      return a[n-3];          }       }      static int[] sort(int a[], int n)   {       int temp=0;       for(int j=0;j<n-2;j++)       {           for(int i=0;i<n-1;i++)       {           if(a[i]>a[i+1])           {               temp=a[i];               a[i] = a[i+1];               a[i+1]=temp;           }       }                  }                     return a;   }
0
krdmicrosoft2 weeks ago
In question, they have specified that Its array of distinct elements, but in test cases they give duplicate elements. because my code fails .😿
 
class Solution
{
    int thirdLargest(int a[], int n)
    {
	    // Your code here
	    
	    if(n<3) return -1;
	    int max = Integer.MIN_VALUE;
	    for(int i = 0;i< n;i++)
	    {
	        if(a[i]>max) max = a[i];
	    }
	    int secMax = Integer.MIN_VALUE;
	    
	    for(int i = 0;i< n;i++)
	    {
	        if(a[i]>secMax && a[i]<max) secMax = a[i];
	    }
	    
	    int thdMax = Integer.MIN_VALUE;
	    for(int i = 0;i< n;i++)
	    {
	        if(a[i]>thdMax && a[i]<secMax) thdMax = a[i];
	    }
	    
	    return thdMax;
	    
    }
}
0
anu1492642 weeks ago
JAVA SOLUTION..............
 
class Solution{   int thirdLargest(int a[], int n)   {    // Your code here    if(n <= 2){        return -1;    }    Arrays.sort(a);    return a[n-3];   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.