Third largest element in an array of distinct elements | PHP program to find third Largest element in an array of distinct elements ; There should be atleast three elements ; Find first largest element ; Find second largest element ; Find third largest element ; Driver Code
< ? php function thirdLargest ( $ arr , $ arr_size ) { if ( $ arr_size < 3 ) { echo " ▁ Invalid ▁ Input ▁ " ; return ; } $ first = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ arr_size ; $ i ++ ) if ( $ arr [ $ i ] > $ first ) $ first = $ arr [ $ i ] ; $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) if ( $ arr [ $ i ] > $ second && $ arr [ $ i ] < $ first ) $ second = $ arr [ $ i ] ; $ third = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) if ( $ arr [ $ i ] > $ third && $ arr [ $ i ] < $ second ) $ third = $ arr [ $ i ] ; echo " The ▁ third ▁ Largest ▁ element ▁ is ▁ " , $ third , " STRNEWLINE " ; } $ arr = array ( 12 , 13 , 1 , 10 , 34 , 16 ) ; $ n = sizeof ( $ arr ) ; thirdLargest ( $ arr , $ n ) ; ? >