


Given an array/list 'ARR' of ‘N’ distinct integers, you are supposed to find the third largest element in the given array 'ARR'.
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a single integer ‘N’ denoting the number of elements in the array/list.
The second line of each test case contains ‘N’ single space-separated distinct integers denoting the elements of 'ARR'.
Output Format :
For each test case, print the third largest element in the given array/list 'ARR'.
Note :
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 50
3 <= N <= 10^4
-10^5 <= ARR[i] <= 10^5
Where 'ARR[i]' denotes the i-th elements of the given array/list.
Time Limit: 1 sec
2
5
2 6 7 4 9
4
7 2 5 4
6
4
In the first test case, if we arrange the array in non decreasing order we will get { 2, 4, 6, 7, 9}. Thus the third largest element is 6.
In the second test case, the third largest element is 4.
2
5
1 -4 2 5 3
3
3 5 6
2
3
In the first test case, the third largest element is 2.
In the second test case, the third largest element is 3.
Can you think about sorting?
The idea is to sort the array in non-decreasing order, and then return the third element from the back of the array.
The steps are as follows :
O( N * log(N) ), where ‘N’ is the number of elements in the given array/list.
We are sorting the array which will take O( N * log(N) ) time. Thus, the overall time complexity is O( N * log(N) ).
O(1)
We are not using any extra space. Thus, the overall space complexity will be O(1).