Third largest element ( without sorting )
PROBLEM :
Given an array of distinct elements, Your task is to find the third largest element in it. You have to complete the function thirdLargest which takes two argument . The first argument is the array a[] and the second argument is the size of the array (n). The function returns an integer denoting the third largest element in the array a[].
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow . The first line of each test case is N,N is the size of array.The second line of each test case contains N space separated values of the array a[ ].
Output:
Output for each test case will be the third largest element of the array .
Constraints:
1 = T = 100
1 = N = 100
1 = A[ ] = 100
Example(To be used for only expected output):
Input:
1
5
2 4 1 3 5
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*you are required to complete this function*/
int thirdLargest(int a[],int n)
{
if(n<3)
return -1 ;
int i ;
int first,second,third ;
first=a[0] ;
second=INT_MIN ;
third=INT_MIN ;
for(i=1;i<n;i++)
{
if(a[i]>first)
{
third=second ;
second=first ;
first=a[i] ;
}
else if(a[i]>second)
{
third=second ;
second=a[i] ;
}
else if(a[i]>third)
third=a[i] ;
}
return third ;
}
---------------------------------------------------------------------------------PROBLEM :
Given an array of distinct elements, Your task is to find the third largest element in it. You have to complete the function thirdLargest which takes two argument . The first argument is the array a[] and the second argument is the size of the array (n). The function returns an integer denoting the third largest element in the array a[].
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow . The first line of each test case is N,N is the size of array.The second line of each test case contains N space separated values of the array a[ ].
Output:
Output for each test case will be the third largest element of the array .
Constraints:
1 = T = 100
1 = N = 100
1 = A[ ] = 100
Example(To be used for only expected output):
Input:
1
5
2 4 1 3 5
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*you are required to complete this function*/
int thirdLargest(int a[],int n)
{
if(n<3)
return -1 ;
int i ;
int first,second,third ;
first=a[0] ;
second=INT_MIN ;
third=INT_MIN ;
for(i=1;i<n;i++)
{
if(a[i]>first)
{
third=second ;
second=first ;
first=a[i] ;
}
else if(a[i]>second)
{
third=second ;
second=a[i] ;
}
else if(a[i]>third)
third=a[i] ;
}
return third ;
}
---------------------------------------------------------------------------------
Given an array of distinct elements, Your task is to find the third largest element in it. You have to complete the function thirdLargest which takes two argument . The first argument is the array a[] and the second argument is the size of the array (n). The function returns an integer denoting the third largest element in the array a[].
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow . The first line of each test case is N,N is the size of array.The second line of each test case contains N space separated values of the array a[ ].
Output:
Output for each test case will be the third largest element of the array .
Constraints:
1 = T = 100
1 = N = 100
1 = A[ ] = 100
Example(To be used for only expected output):
Input:
1
5
2 4 1 3 5
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*you are required to complete this function*/
int thirdLargest(int a[],int n)
{
if(n<3)
return -1 ;
int i ;
int first,second,third ;
first=a[0] ;
second=INT_MIN ;
third=INT_MIN ;
for(i=1;i<n;i++)
{
if(a[i]>first)
{
third=second ;
second=first ;
first=a[i] ;
}
else if(a[i]>second)
{
third=second ;
second=a[i] ;
}
else if(a[i]>third)
third=a[i] ;
}
return third ;
}
---------------------------------------------------------------------------------PROBLEM :
Given an array of distinct elements, Your task is to find the third largest element in it. You have to complete the function thirdLargest which takes two argument . The first argument is the array a[] and the second argument is the size of the array (n). The function returns an integer denoting the third largest element in the array a[].
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow . The first line of each test case is N,N is the size of array.The second line of each test case contains N space separated values of the array a[ ].
Output:
Output for each test case will be the third largest element of the array .
Constraints:
1 = T = 100
1 = N = 100
1 = A[ ] = 100
Example(To be used for only expected output):
Input:
1
5
2 4 1 3 5
Output:
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*you are required to complete this function*/
int thirdLargest(int a[],int n)
{
if(n<3)
return -1 ;
int i ;
int first,second,third ;
first=a[0] ;
second=INT_MIN ;
third=INT_MIN ;
for(i=1;i<n;i++)
{
if(a[i]>first)
{
third=second ;
second=first ;
first=a[i] ;
}
else if(a[i]>second)
{
third=second ;
second=a[i] ;
}
else if(a[i]>third)
third=a[i] ;
}
return third ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment