![]() |
VOOZH | about |
Given an array arr[], the task is to find the top three largest distinct integers present in the array.
Note: If there are less than three distinct elements in the array, then return the available distinct numbers in descending order.
Examples :
Input: arr[] = [10, 4, 3, 50, 23, 90]
Output: [90, 50, 23]Input: arr[] = [10, 9, 9]
Output: [10, 9]
There are only two distinct elementsInput: arr[] = [10, 10, 10]
Output: [10]
There is only one distinct element
Table of Content
First find the maximum in one pass, then find the maximum excluding it for the second largest, and again exclude both to get the third largest.
34 13 12
Keep track of the three largest values while scanning the array once, updating their positions whenever a bigger element is found, so the largest, second largest, and third largest are always maintained in order.
34 13 12