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
[Naive Approach] Three Traversals - O(n) Time and O(1) Space
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.
#include <bits/stdc++.h>
using namespace std;
vector<int> get3largest(vector<int> &arr)
{
int fst = INT_MIN, sec = INT_MIN, thd = INT_MIN;
// Find the first largest element
for (int x : arr)
{
if (x > fst)
fst = x;
}
// Find the second largest element
for (int x : arr)
{
if (x > sec && x != fst)
sec = x;
}
// Find the third largest element
for (int x : arr)
{
if (x > thd && x != fst && x != sec)
thd = x;
}
vector<int> res;
if (fst == INT_MIN) return res;
res.push_back(fst);
if (sec == INT_MIN) return res;
res.push_back(sec);
if (thd == INT_MIN) return res;
res.push_back(thd);
return res;
}
int main()
{
vector<int> arr = {12, 13, 1, 10, 34, 1};
vector<int> res = get3largest(arr);
for (int x : res)
cout << x << " ";
cout << endl;
return 0;
}
#include <stdio.h>
#include <limits.h>
void get3largest(int arr[], int n)
{
int fst = INT_MIN, sec = INT_MIN, thd = INT_MIN;
// Find the first largest element
for (int i = 0; i < n; i++)
{
if (arr[i] > fst)
fst = arr[i];
}
// Find the second largest element
for (int i = 0; i < n; i++)
{
if (arr[i] > sec && arr[i] != fst)
sec = arr[i];
}
// Find the third largest element
for (int i = 0; i < n; i++)
{
if (arr[i] > thd && arr[i] != fst && arr[i] != sec)
thd = arr[i];
}
if (fst != INT_MIN) printf("%d ", fst);
if (sec != INT_MIN) printf("%d ", sec);
if (thd != INT_MIN) printf("%d ", thd);
printf("\n");
}
int main()
{
int arr[] = {12, 13, 1, 10, 34, 1};
int n = sizeof(arr) / sizeof(arr[0]);
get3largest(arr, n);
return 0;
}
import java.util.ArrayList;
class GFG
{
static ArrayList<Integer> get3largest(int[] arr)
{
int fst = Integer.MIN_VALUE;
int sec = Integer.MIN_VALUE;
int thd = Integer.MIN_VALUE;
// Find the first largest element
for (int x : arr)
{
if (x > fst)
fst = x;
}
// Find the second largest element
for (int x : arr)
{
if (x > sec && x != fst)
sec = x;
}
// Find the third largest element
for (int x : arr)
{
if (x > thd && x != fst && x != sec)
thd = x;
}
ArrayList<Integer> res = new ArrayList<>();
if (fst == Integer.MIN_VALUE) return res;
res.add(fst);
if (sec == Integer.MIN_VALUE) return res;
res.add(sec);
if (thd == Integer.MIN_VALUE) return res;
res.add(thd);
return res;
}
public static void main(String[] args)
{
int[] arr = {12, 13, 1, 10, 34, 1};
ArrayList<Integer> res = get3largest(arr);
for (int x : res)
System.out.print(x + " ");
System.out.println();
}
}
def get3largest(arr):
fst = float('-inf')
sec = float('-inf')
thd = float('-inf')
# Find the first largest element
for x in arr:
if x > fst:
fst = x
# Find the second largest element
for x in arr:
if x > sec and x != fst:
sec = x
# Find the third largest element
for x in arr:
if x > thd and x != fst and x != sec:
thd = x
res = []
if fst == float('-inf'):
return res
res.append(fst)
if sec == float('-inf'):
return res
res.append(sec)
if thd == float('-inf'):
return res
res.append(thd)
return res
if __name__ == "__main__":
arr = [12, 13, 1, 10, 34, 1]
res = get3largest(arr)
print(" ".join(map(str, res)))
using System;
using System.Collections.Generic;
class GFG
{
static List<int> get3largest(int[] arr)
{
int fst = int.MinValue;
int sec = int.MinValue;
int thd = int.MinValue;
// Find the first largest element
foreach (int x in arr)
{
if (x > fst)
fst = x;
}
// Find the second largest element
foreach (int x in arr)
{
if (x > sec && x != fst)
sec = x;
}
// Find the third largest element
foreach (int x in arr)
{
if (x > thd && x != fst && x != sec)
thd = x;
}
List<int> res = new List<int>();
if (fst == int.MinValue) return res;
res.Add(fst);
if (sec == int.MinValue) return res;
res.Add(sec);
if (thd == int.MinValue) return res;
res.Add(thd);
return res;
}
static void Main()
{
int[] arr = {12, 13, 1, 10, 34, 1};
List<int> res = get3largest(arr);
foreach (int x in res)
Console.Write(x + " ");
Console.WriteLine();
}
}
function get3largest(arr)
{
let fst = -Infinity;
let sec = -Infinity;
let thd = -Infinity;
// Find the first largest element
arr.forEach(x => {
if (x > fst)
fst = x;
});
// Find the second largest element
arr.forEach(x => {
if (x > sec && x !== fst)
sec = x;
});
// Find the third largest element
arr.forEach(x => {
if (x > thd && x !== fst && x !== sec)
thd = x;
});
let res = [];
if (fst !== -Infinity) res.push(fst);
if (sec !== -Infinity) res.push(sec);
if (thd !== -Infinity) res.push(thd);
return res;
}
let arr = [12, 13, 1, 10, 34, 1];
let res = get3largest(arr);
console.log(res.join(" "));
Output
34 13 12
[Efficient Approach] One Traversal - O(n) Time and O(1) Space
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.
#include <bits/stdc++.h>
using namespace std;
vector<int> get3largest(vector<int> &arr)
{
int fst = INT_MIN, sec = INT_MIN, thd = INT_MIN;
for (int x : arr)
{
// If current element is greater than fst
if (x > fst)
{
thd = sec;
sec = fst;
fst = x;
}
// If x is between fst and sec
else if (x > sec && x != fst)
{
thd = sec;
sec = x;
}
// If x is between sec and thd
else if (x > thd && x != sec && x != fst)
thd = x;
}
vector<int> res = {};
if (fst == INT_MIN)
return res;
res.push_back(fst);
if (sec == INT_MIN)
return res;
res.push_back(sec);
if (thd == INT_MIN)
return res;
res.push_back(thd);
return res;
}
// Driver code
int main()
{
vector<int> arr = {12, 13, 1, 10, 34, 1};
vector<int> res = get3largest(arr);
for (int x : res)
cout << x << " ";
cout << endl;
return 0;
}
#include <stdio.h>
#include <limits.h>
void get3largest(int arr[], int n) {
int fst = INT_MIN, sec = INT_MIN, thd = INT_MIN;
for (int i = 0; i < n; i++) {
int x = arr[i];
// If current element is greater than fst
if (x > fst) {
thd = sec;
sec = fst;
fst = x;
}
// If x is between fst and sec
else if (x > sec && x != fst) {
thd = sec;
sec = x;
}
// If x is between sec and thd
else if (x > thd && x != sec && x != fst) {
thd = x;
}
}
if (fst != INT_MIN) printf("%d ", fst);
if (sec != INT_MIN) printf("%d ", sec);
if (thd != INT_MIN) printf("%d ", thd);
printf("\n");
}
int main() {
int arr[] = {12, 13, 1, 10, 34, 1};
int n = sizeof(arr) / sizeof(arr[0]);
get3largest(arr, n);
return 0;
}
import java.util.*;
class GFG {
public static ArrayList<Integer> get3largest(int[] arr) {
int fst = Integer.MIN_VALUE, sec = Integer.MIN_VALUE, thd = Integer.MIN_VALUE;
for (int x : arr) {
// If current element is greater than fst
if (x > fst) {
thd = sec;
sec = fst;
fst = x;
}
// If x is between fst and sec
else if (x > sec && x != fst) {
thd = sec;
sec = x;
}
// If x is between sec and thd
else if (x > thd && x != sec && x != fst) {
thd = x;
}
}
ArrayList<Integer> res = new ArrayList<>();
if (fst == Integer.MIN_VALUE) return res;
res.add(fst);
if (sec == Integer.MIN_VALUE) return res;
res.add(sec);
if (thd == Integer.MIN_VALUE) return res;
res.add(thd);
return res;
}
public static void main(String[] args) {
int[] arr = {12, 13, 1, 10, 34, 1};
ArrayList<Integer> res = get3largest(arr);
for (int x : res) {
System.out.print(x + " ");
}
System.out.println();
}
}
def get3largest(arr):
fst = sec = thd = float('-inf')
for x in arr:
# If current element is greater than fst
if x > fst:
thd = sec
sec = fst
fst = x
# If x is between fst and sec
elif x > sec and x != fst:
thd = sec
sec = x
# If x is between sec and thd
elif x > thd and x != sec and x != fst:
thd = x
res = []
if fst == float('-inf'):
return res
res.append(fst)
if sec == float('-inf'):
return res
res.append(sec)
if thd == float('-inf'):
return res
res.append(thd)
return res
# Driver code
arr = [12, 13, 1, 10, 34, 1]
res = get3largest(arr)
print(" ".join(map(str, res)))
using System;
using System.Collections.Generic;
class GFG
{
public static List<int> get3largest(int[] arr)
{
int fst = int.MinValue, sec = int.MinValue, thd = int.MinValue;
foreach (int x in arr)
{
// If current element is greater than fst
if (x > fst)
{
thd = sec;
sec = fst;
fst = x;
}
// If x is between fst and sec
else if (x > sec && x != fst)
{
thd = sec;
sec = x;
}
// If x is between sec and thd
else if (x > thd && x != sec && x != fst)
thd = x;
}
List<int> res = new List<int>();
if (fst == int.MinValue) return res;
res.Add(fst);
if (sec == int.MinValue) return res;
res.Add(sec);
if (thd == int.MinValue) return res;
res.Add(thd);
return res;
}
static void Main()
{
int[] arr = {12, 13, 1, 10, 34, 1};
List<int> res = get3largest(arr);
foreach (int x in res)
Console.Write(x + " ");
Console.WriteLine();
}
}
function get3largest(arr) {
let fst = -Infinity, sec = -Infinity, thd = -Infinity;
arr.forEach(x => {
// If current element is greater than fst
if (x > fst) {
thd = sec;
sec = fst;
fst = x;
}
// If x is between fst and sec
else if (x > sec && x !== fst) {
thd = sec;
sec = x;
}
// If x is between sec and thd
else if (x > thd && x !== sec && x !== fst) {
thd = x;
}
});
let res = [];
if (fst !== -Infinity) res.push(fst);
if (sec !== -Infinity) res.push(sec);
if (thd !== -Infinity) res.push(thd);
return res;
}
// Driver code
let arr = [12, 13, 1, 10, 34, 1];
let res = get3largest(arr);
console.log(res.join(' '));
Output
34 13 12