Third greatest element

Given an array/list 'ARR' of ‘N’ distinct integers, you are supposed to find the third largest element in the given array 'ARR'.

Input Format :
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.
Constraints :
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
CodingNinjas
author
2y
Sorting Based Approach

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 :

  1. Sort the array in non-decreasing or...read more
CodingNinjas
author
2y
Multiple Traversals Approach

The idea is to iterate through the array and find the largest element and the second-largest elements in the given array/list and then again iterate to find the third large...read more

CodingNinjas
author
2y
Single Traversal Approach

The idea is to iterate through the array only once and keep track of the largest element, second largest, and the third-largest element simultaneously.

The steps are as follo...read more

Add answer anonymously...
Freshworks Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter