# Possible to form a triangle from array values

Given an array of integers, we need to find out whether it is possible to construct at least one non-degenerate triangle using array values as its sides. In other words, we need to find out 3 such array indices which can become sides of a non-degenerate triangle.
Examples :

```Input : [4, 1, 2]
Output : No
No triangle is possible from given
array values

Input : [5, 4, 3, 1, 2]
Output : Yes
Sides of possible triangle are 2 3 4
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

For a non-degenerate triangle, its sides should follow these constraints,

```A + B > C    and
B + C > A    and
C + A > B
where A, B and C are length of sides of the triangle.
```

The task is to find any triplet from array that satisfies above condition.

A Simple Solution is to generate all triplets and for every triplet check if it forms a triangle or not by checking above three conditions.

An Efficient Solution is use sorting. First, we sort the array then we loop once and we will check three consecutive elements of this array if any triplet satisfies arr[i] + arr[i+1] > arr[i+2], then we will output that triplet as our final result.

Why checking only 3 consecutive elements will work instead of trying all possible triplets of sorted array?
Let we are at index i and 3 line segments are arr[i], arr[i + 1] and arr[i + 2] with relation arr[i] < arr[i+1] < arr[i+2], If they can't form a non-degenerate triangle, Line segments of lengths arr[i-1], arr[i+1] and arr[i+2] or arr[i], arr[i+1] and arr[i+3] can't form a non-degenerate triangle also because sum of arr[i-1] and arr[i+1] will be even less than sum of arr[i] and arr[i+1] in first case and sum of arr[i] and arr[i+1] must be less than arr[i+3] in second case, So we don't need to try all the combinations, we will try just 3 consecutive indices of array in sorted form.
The total complexity of below solution is O(n log n)

## C++

 `// C++ program to find if it  ` `// is possible to form a  ` `// triangle from array values ` `#include ` `using` `namespace` `std; ` ` `  `// Method prints possible  ` `// triangle when array values ` `// are taken as sides ` `bool` `isPossibleTriangle(``int` `arr[],  ` `                        ``int` `N) ` `{ ` `    ``// If number of elements are  ` `    ``// less than 3, then no  ` `    ``// triangle is possible ` `    ``if` `(N < 3) ` `    ``return` `false``; ` ` `  `    ``// first sort the array ` `    ``sort(arr, arr + N); ` ` `  `    ``// loop for all 3  ` `    ``// consecutive triplets ` `    ``for` `(``int` `i = 0; i < N - 2; i++) ` ` `  `        ``// If triplet satisfies  ` `        ``// triangle condition, break ` `        ``if` `(arr[i] + arr[i + 1] > arr[i + 2]) ` `            ``return` `true``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = {5, 4, 3, 1, 2}; ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(``int``); ` ` `  `    ``isPossibleTriangle(arr, N) ? cout << ``"Yes"` `: ` `                                 ``cout << ``"No"``; ` `    ``return` `0; ` `} `

## JAVA

 `// Java program to find if it is  ` `// possible to form a triangle  ` `// from array values ` `import` `java.io.*; ` `import` `java.util.Arrays; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Method prints possible  ` `    ``// triangle when array values ` `    ``// are taken as sides ` `    ``static` `boolean` `isPossibleTriangle(``int` `[]arr, ` `                                      ``int` `N) ` `    ``{ ` `         `  `        ``// If number of elements are  ` `        ``// less than 3, then no  ` `        ``// triangle is possible ` `        ``if` `(N < ``3``) ` `            ``return` `false``; ` `     `  `        ``// first sort the array ` `        ``Arrays.sort(arr); ` `     `  `        ``// loop for all 3  ` `        ``// consecutive triplets ` `        ``for` `(``int` `i = ``0``; i < N - ``2``; i++) ` `     `  `            ``// If triplet satisfies  ` `            ``// triangle condition, break ` `            ``if` `(arr[i] + arr[i + ``1``] > arr[i + ``2``]) ` `                ``return` `true``; ` `                 `  `        ``return` `false``; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``static` `public` `void` `main (String[] args) ` `    ``{ ` `        ``int` `[]arr = {``5``, ``4``, ``3``, ``1``, ``2``}; ` `        ``int` `N = arr.length; ` `         `  `        ``if``(isPossibleTriangle(arr, N)) ` `            ``System.out.println(``"Yes"` `); ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python

 `# Python3 code to find if  ` `# it is possible to form a  ` `# triangle from array values ` ` `  `# Method prints possible  ` `# triangle when array  ` `# values are taken as sides ` `def` `isPossibleTriangle (arr , N): ` `     `  `    ``# If number of elements  ` `    ``# are less than 3, then  ` `    ``# no triangle is possible ` `    ``if` `N < ``3``: ` `        ``return` `False` `     `  `    ``# first sort the array ` `    ``arr.sort() ` `     `  `    ``# loop for all 3  ` `    ``# consecutive triplets ` `    ``for` `i ``in` `range``(N ``-` `2``): ` `         `  `        ``# If triplet satisfies triangle ` `        ``# condition, break ` `        ``if` `arr[i] ``+` `arr[i ``+` `1``] > arr[i ``+` `2``]: ` `            ``return` `True` ` `  `# Driver Code ` `arr ``=` `[``5``, ``4``, ``3``, ``1``, ``2``] ` `N ``=` `len``(arr)  ` `print``(``"Yes"` `if` `isPossibleTriangle(arr, N) ``else` `"No"``) ` ` `  `# This code is contributed  ` `# by "Sharad_Bhardwaj". `

## C#

 `// C# program to find if  ` `// it is possible to form ` `// a triangle from array values ` `using` `System; ` ` `  `class` `GFG ` `{ ` `         `  `    ``// Method prints possible  ` `    ``// triangle when array values ` `    ``// are taken as sides ` `    ``static` `bool` `isPossibleTriangle(``int` `[]arr, ` `                                   ``int` `N) ` `    ``{ ` `        ``// If number of elements  ` `        ``// are less than 3, then  ` `        ``// no triangle is possible ` `        ``if` `(N < 3) ` `            ``return` `false``; ` `     `  `        ``// first sort the array ` `        ``Array.Sort(arr); ` `     `  `        ``// loop for all 3  ` `        ``// consecutive triplets ` `        ``for` `(``int` `i = 0; i < N - 2; i++) ` `     `  `            ``// If triplet satisfies triangle ` `            ``// condition, break ` `            ``if` `(arr[i] + arr[i + 1] > arr[i + 2]) ` `                ``return` `true``; ` `                 `  `        ``return` `false``; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``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"``); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` ``\$arr``[``\$i` `+ 2]) ` `            ``return` `true; ` `} ` ` `  `// Driver Code ` `\$arr` `= ``array``(5, 4, 3, 1, 2); ` `\$N` `= ``count``(``\$arr``); ` ` `  `if``(isPossibleTriangle(``\$arr``,``\$N``)) ` `echo` `"Yes"` `; ` `else`                 `echo` `"No"``; ` ` `  `// This code is contributed by vt_m ` `?> `

Output :

```Yes
```

This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : vt_m