Asked inFidelity International,Full Stack Engineering Intern

Second Largest Element Problem

You are provided with an array ARR of integers. The objective is to determine the second largest element in ARR.

Note:

a) Duplicate elements are allowed.
b) If no second largest element exists, return -1.

Input:

The first line contains an integer T, the number of test cases.
For each test case, the first line contains an integer N, the number of elements in the array.
The second line contains N space-separated integers representing the elements of the array.

Output:

Output a single integer for each test case, the second largest element of the array. Output each result on a new line.

Example:

Input:
2
5
2 4 5 6 8
4
1 1 1 1
Output:
6
-1
Explanation:

In the first test case, 8 is the largest number and 6 is the second-largest.

In the second test case, all numbers are the same, so there is no second largest value.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 5000
  • -10^9 ≤ ARR[i] ≤ 10^9

Ensure your solution handles large arrays efficiently within a time limit of 1 second.

Be the first one to answer
Add answer anonymously...
Fidelity International Full Stack Engineering Intern 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

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