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[]. The function should return **-1** if there are less than 3 elements in input.

**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, 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 ≤ 10^{3}

1 ≤ A[i] ≤ 10^{3}

**Example(To be used for only expected output):**

**Input:**

1

5

2 4 1 3 5

**Output:**

3

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

hardikJain5 | 258 |

CodeBuddy | 250 |

bhatabhi554 | 232 |

Akkki111 | 197 |

clone | 174 |

KshatriyaYash | 1932 |

nikhil_sojan | 1374 |

lonecoder | 1236 |

SumitSingh27 | 1127 |

mazumderrohit8 | 1124 |

blackshadows | 5327 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4591 |

Quandray | 4444 |

Login to report an issue on this page.