Possible to form a triangle from array values | Method prints possible triangle when array values are taken as sides ; If number of elements are less than 3 , then no triangle is possible ; first sort the array ; loop for all 3 consecutive triplets ; If triplet satisfies triangle condition , break ; Driver code
function isPossibleTriangle ( arr , N ) { if ( N < 3 ) return false ; arr . sort ( ) ; for ( let i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; return false ; } let arr = [ 5 , 4 , 3 , 1 , 2 ] ; let N = arr . length ; if ( isPossibleTriangle ( arr , N ) ) document . write ( " " ) ; else document . write ( " " ) ;