text: Largest lexicographic triplet from a given Array that forms a triangle | C ++ Program to implement the the above approach ; 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
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void findTriplet ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; int 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 ) { cout << arr [ i - 2 ] << " ▁ " << arr [ i - 1 ] << " ▁ " << arr [ i ] << endl ; } else { cout << -1 << endl ; } } int main ( ) { int arr [ ] = { 4 , 2 , 10 , 3 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findTriplet ( arr , N ) ; return 0 ; }