Possible to form a triangle from array values | C # program to find if it is 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
using System ; class GFG { static bool isPossibleTriangle ( int [ ] arr , int N ) { if ( N < 3 ) return false ; Array . Sort ( arr ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; return false ; } static public void Main ( ) { int [ ] arr = { 5 , 4 , 3 , 1 , 2 } ; int N = arr . Length ; if ( isPossibleTriangle ( arr , N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }