Largest lexicographic triplet from a given Array that forms a triangle | Function to find lexicographically largest triplet that forms a triangle in the given array ; Sort the array ; Iterate from the end of the array ; If the triplet forms a triangle ; If triplet found ; Print the triplet ; Otherwise ; Driver Code
function findTriplet ( arr , N ) { arr . sort ( ( a , b ) => a - b ) ; var flag = 0 , i ; for ( i = N - 1 ; i - 2 >= 0 ; i -- ) { if ( arr [ i - 2 ] + arr [ i - 1 ] > arr [ i ] ) { flag = 1 ; break ; } } if ( flag ) { document . write ( arr [ i - 2 ] + " " + arr [ i - 1 ] + " " + arr [ i ] + " " ) ; } else { document . write ( - 1 + " " ) ; } } var arr = [ 4 , 2 , 10 , 3 , 5 ] ; var N = arr . length ; findTriplet ( arr , N ) ;