Find the largest three distinct elements in an array | C ++ code to find largest three elements in an array ; It uses Tuned Quicksort with ; avg . case Time complexity = O ( nLogn ) ; to handle duplicate values ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find3largest ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int check = 0 , count = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( count < 4 ) { if ( check != arr [ n - i ] ) { cout << arr [ n - i ] << " ▁ " ; check = arr [ n - i ] ; count ++ ; } } else break ; } } int main ( ) { int arr [ ] = { 12 , 45 , 1 , -1 , 45 , 54 , 23 , 5 , 0 , -10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; find3largest ( arr , n ) ; }