Fourth Largest Element in the Array

Given an array consisting of integers, your task is to determine the fourth largest element in the array. If the array does not contain at least four distinct elements, return the minimum possible value of an integer, which is -2147483648.

Input:

  The first line of input contains the integer 'N', which indicates the number of elements in the array.
The second line contains N space-separated integers that represent the elements of the array.

Output:

  Output the fourth largest integer if it exists, otherwise output -2147483648.

Example:

  Input:
6
2 3 1 5 6 4

Output:
3

Explanation:
The fourth largest element in the sorted array [1, 2, 3, 4, 5, 6] is 3.

Constraints:

  • 1 <= N < 106
  • -106 <= element <= 106
  • Time Limit: 1 sec
Note:
There is no need to explicitly print the output; the function should be implemented to return the answer.
AnswerBot
4mo

Find the fourth largest element in an array, return -2147483648 if not enough distinct elements.

  • Sort the array in descending order

  • Return the fourth element if it exists, else return -2147483648

Help your peers!
Select
Add answer anonymously...

Maersk Software Developer interview questions & answers

A Software Developer was asked 12mo agoQ. What is a decorator in Python?
A Software Developer was asked Q. Architecture of current project
A Software Developer was asked Q. Shortest Path in an Unweighted Graph The city of Ninjaland is represented as an ...read more

Popular interview questions of Software Developer

A Software Developer was asked 12mo agoQ1. What is a decorator in Python?
A Software Developer was asked Q2. Architecture of current project
A Software Developer was asked Q3. Shortest Path in an Unweighted Graph The city of Ninjaland is represented as an ...read more
Maersk Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits