Form a Triangle
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.
Input Format:
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.
Constraints:
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.
CodingNinjas
author
2y
Sort the array and iterate over it to get the anwer
CodingNinjas
author
2y
Brute Force
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:
- 'X' + 'Y' > 'Z'
- 'Y' + 'Z' > 'X'
- 'X' + 'Z...read more
CodingNinjas
author
2y
Sorting approach
The idea is to use sorting and check if the element at any index ‘I’ forms a non-degenerate triangle with elements at index 'I'+1 and 'I'+2 or not.
- Sort the array in nondecreasing orde...read more
Anonymous
1y
#include bool possibleToMakeTriangle(vector &arr) { // Write your code here. int n=arr.size(); if (n<3) return false; sort(arr.begin(),arr.end()); for(int i=0;iread more
Add answer anonymously...
Top PhonePe Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in PhonePe Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app