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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossibleTriangle ( int arr [ ] , int N ) { if ( N < 3 ) return false ; sort ( arr , arr + N ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; return false ; } int main ( ) { int arr [ ] = { 5 , 4 , 3 , 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( int ) ; isPossibleTriangle ( arr , N ) ? cout << " Yes " : cout << " No " ; return 0 ; }