Find the Third Greatest Element
Given an array 'ARR' of 'N' distinct integers, determine the third largest element in the array.
Input:
The first line contains a single integer 'T' representing the number of test cases. Each test case consists of: A single integer 'N', the number of elements in the array, on a new line. Followed by 'N' space-separated distinct integers that comprise the array 'ARR'.
Output:
For each test case, output the third largest element from the array 'ARR'.
Example:
Input:
T = 2
N = 5
ARR = [7, 4, 9, 1, 3]
N = 4
ARR = [10, 20, 30, 40]
Output:
4
20
Constraints:
1 <= T <= 50
3 <= N <= 104
-105 <= ARR[i] <= 105
Note:
You don’t need to print anything; it has been handled. Implement the provided function to solve the problem.

AnswerBot
4mo
Find the third largest element in an array of distinct integers.
Sort the array in descending order and return the element at index 2.
Handle cases where the array has less than 3 elements separately.
Co...read more
MD Adil
1y
public class ThirdLargestArrayElement {
public static void main(String[] args) {
int a[] ={10,20,30,50,60,5,90};
int max1 = 0;
int max2 = Integer.MIN_VALUE;
int max3 = Integer.MIN_VALUE;
for(int i = 0; imax1){
max3 = max2;
max2 = max1;
max1 = a[i];
} else if (a[i]> max2) {
max3 = max2;
max2 = a[i];
} else if (a[i] > max3) {
max3= a[i];
}
}
System.out.println("Third largest Element in array" + " " + max3);
}
}
Help your peers!
Add answer anonymously...
Jupiter Money Software Developer interview questions & answers
A Software Developer was asked Q. Design a Chess game using object-oriented principles.
A Software Developer was asked Q. Shortest Path in a Binary Matrix Problem Statement Given a binary matrix of size...read more
A Software Developer was asked Q. Can you explain how you would design a URL shortener?
Popular interview questions of Software Developer
A Software Developer was asked Q1. Design a Chess game using object-oriented principles.
A Software Developer was asked Q2. Shortest Path in a Binary Matrix Problem Statement Given a binary matrix of size...read more
A Software Developer was asked Q3. Can you explain how you would design a URL shortener?
>
Jupiter Money Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

