📌  相关文章
📜  可以从数组值形成三角形

📅  最后修改于: 2021-05-04 23:54:04             🧑  作者: Mango

给定一个整数数组,我们需要找出是否有可能使用数组值作为边来构造至少一个非退化三角形。换句话说,我们需要找出3个这样的数组索引,它们可以成为非退化三角形的边。
例子 :

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

对于非退化三角形,其边应遵循这些约束,

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

任务是从满足上述条件的数组中查找任何三元组。
一个简单的解决方案是生成所有三元组,并通过检查以上三个条件来检查每个三元组是否形成三角形。
一种有效的解决方案是使用排序。首先,我们对数组进行排序,然后循环一次,如果该三元组满足arr [i] + arr [i + 1]> arr [i + 2],我们将检查该数组的三个连续元素,然后将三元组输出为我们的最终结果。
为什么只检查3个连续元素而不是尝试所有可能的已排序数组的三元组?
让我们在索引i处,有3个线段分别是arr [i],arr [i + 1]和arr [i + 2],它们的关系arr [i] 以下解决方案的总复杂度为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;
return false;
}
 
// 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
?>


Javascript


输出 :

Yes