Problem of the day
You are given an integer of array/list 'ARR' of length ‘N. You are supposed to return true if it is possible to construct at least one non-degenerate triangle using values of array/list as sides of the triangle, otherwise, return false.
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a single integer ‘N’ denoting the number of elements in the array.
The second line contains ‘N’ single space-separated integers denoting the elements of the array/list.
Output Format:
For each test case, print a single line containing either “YES”(without quotes) if it is possible to form a non-degenerate triangle, otherwise “NO”(without quotes).
The output of each test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 100
3 <= N <= 10 ^ 3
0 <= ARR[i] <= 10 ^ 9
Where ‘T’ denotes the number of test cases, ‘N’ denotes the number of elements in the array, and 'ARR[i] denotes the elements of the array.
Time Limit: 1 sec.
2
5
4 2 1 3 2
5
5 2 7 3 15
YES
YES
In the first test case, if we choose the sides as { 2,3,4} or {2,2,1} or {2,2,3} then it is possible to form a non-degenerate triangle.
In the second test case, if we choose sides as {5,3,7}, then it is possible to form a non-degenerate triangle.
2
5
12 3 7 4 28
4
7 12 9 20
NO
YES
In the first test case, there is no possible way to choose three elements such that they will form the sides of a triangle.
In the second test case, if we choose the sides as {7,12,9} or {12,9,20}, then it is possible to form a non-degenerate triangle
When can we make a non-degenerate triangle from any combination of 3 elements?
Suppose that 'X', 'Y' and 'Z' are the sides of a triangle, so a non-degenerate triangle can be formed if the following three conditions are satisfied:
The idea is to explore all the possible ways of choosing 3 elements from the given array/list and check if any combination satisfies the constraints for making a non-degenerate triangle or not.
1. 'ARR'[I] +'ARR'[J] >'ARR'[K]
2. 'ARR'[J] + 'ARR'[K] > 'ARR'[I]
3. 'ARR'[I] + 'ARR'[K] > 'ARR'[J]
If yes, then return true otherwise we will go to the next triplet.
If no triplet can become sides of a non-degenerate triangle, return false.
O(N ^ 3), where ‘N’ is the number of elements in the given array/list.
We are pivoting every element as the first side of the triangle and for each first side of the triangle we are going to pivot all the element from next of the first side as the second side of the triangle and for every second side, we are pivoting next element from the second side, as the third side of the triangle. So in the worst case, when there is no triplet that forms a non-degenerate triangle, we have to check all the triplets. Thus, the total time complexity will be O(N ^ 3).
O(1).
We are not using any extra space. Thus, the space complexity will be O(1).