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.
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
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)
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 firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of possible pairs of Hypotenuse and Area to form right angled triangle
- All possible values of floor(N/K) for all values of K
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Program to print a Hollow Triangle inside a Triangle
- Probability of cutting a rope into three pieces such that the sides form a triangle
- Check if right triangle possible from given area and hypotenuse
- Check if a triangle of positive area is possible with the given angles
- Minimum and maximum possible length of the third side of a triangle
- Check if Pascal's Triangle is possible with a complete layer by using numbers upto N
- Sum of values of all possible non-empty subsets of the given array
- Product of values of all possible non-empty subsets of given Array
- Sort first k values in ascending order and remaining n-k values in descending order
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Count of all possible values of X such that A % X = B
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Probability of getting all possible values on throwing N dices
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count possible values of K such that X - 1 and Y - 1 modulo K is same