📜  阵列的最大周长三角形

📅  最后修改于: 2021-05-06 10:30:38             🧑  作者: Mango

给定非负整数数组。从此数组中找出形成最大周长的三角形的三个元素。
例子 :

Input : {6, 1, 6, 5, 8, 4}
Output : 20

Input : {2, 20, 7, 55, 1, 33, 12, 4}
Output : Triangle formation is not possible.

Input: {33, 6, 20, 1, 8, 12, 5, 55, 4, 9}
Output: 41

天真的解决方案:
蛮力解决方案是:检查3个元素的所有组合是否形成三角形,如果形成三角形则更新最大周长。天真的解决方案的复杂度是O(n 3 )。下面是它的代码。

C++
// Brute force solution to find
// out maximum perimeter triangle which
// can be formed using the elements
// of the given array
#include 
#include 
 
using namespace std;
 
// Function to find out maximum perimeter
void maxPerimeter(int arr[], int n){
 
    // initialize maximum perimeter
    // as 0.
    int maxi = 0;
 
    // pick up 3 different elements
    // from the array.
    for (int i = 0; i < n - 2; i++){
        for (int j = i + 1; j < n - 1; j++){
            for (int k = j + 1; k < n; k++){
 
                // a, b, c are 3 sides of the triangle
                int a = arr[i];
                int b = arr[j];
                int c = arr[k];
 
                // check whether a, b, c forms
                // a triangle or not.
                if (a < b+c && b < c+a && c < a+b){
 
                    // if it forms a triangle
                    // then update the maximum value.
                    maxi = max(maxi, a+b+c);
                }
            }
        }
    }
 
    // If maximum perimeter is non-zero
    // then print it.
    if (maxi) cout << "Maximum Perimeter is: "
                   << maxi << endl;
 
    // otherwise no triangle formation
    // is possible.
    else cout << "Triangle formation "
        << "is not possible." << endl;
}
 
// Driver Program
int main()
{
    // test case 1
    int arr1[6] = {6, 1, 6, 5, 8, 4};
    maxPerimeter(arr1, 6);
 
    // test case 2
    int arr2[8] = {2, 20, 7, 55, 1,
                    33, 12, 4};
    maxPerimeter(arr2, 8);
 
    // test case 3
    int arr3[10] = {33, 6, 20, 1, 8,
                    12, 5, 55, 4, 9};
    maxPerimeter(arr3, 10);
 
    return 0;
}


Java
// Brute force solution to find out maximum
// perimeter triangle which can be formed
// using the elements of the given array
import java.io.*;
 
class GFG {
 
    // Function to find out maximum perimeter
    static void maxPerimeter(int arr[], int n)
    {
     
        // initialize maximum perimeter as 0.
        int maxi = 0;
     
        // pick up 3 different elements
        // from the array.
        for (int i = 0; i < n - 2; i++)
        {
            for (int j = i + 1; j < n - 1; j++)
            {
                for (int k = j + 1; k < n; k++)
                {
     
                    // a, b, c are 3 sides of
                    // the triangle
                    int a = arr[i];
                    int b = arr[j];
                    int c = arr[k];
     
                    // check whether a, b, c
                    // forms a triangle or not.
                    if (a < b+c && b < c+a && c < a+b)
                    {
     
                        // if it forms a triangle
                        // then update the maximum
                        // value.
                        maxi = Math.max(maxi, a+b+c);
                    }
                }
            }
        }
     
        // If maximum perimeter is non-zero
        // then print it.
        if (maxi > 0)
        System.out.println( "Maximum Perimeter is: "
                                             + maxi);
     
        // otherwise no triangle formation
        // is possible.
        else
        System.out.println( "Triangle formation "
                              + "is not possible." );
    }
     
    // Driver Program
    public static void main (String[] args)
    {
         
        // test case 1
        int arr1[] = {6, 1, 6, 5, 8, 4};
        maxPerimeter(arr1, 6);
     
        // test case 2
        int arr2[] = {2, 20, 7, 55, 1, 33, 12, 4};
        maxPerimeter(arr2, 8);
     
        // test case 3
        int arr3[] = {33, 6, 20, 1, 8,
                                12, 5, 55, 4, 9};
        maxPerimeter(arr3, 10);
    }
}
 
// This code is contributed by anuj_67.


Python
# Brute force solution to find
# out maximum perimeter triangle
# which can be formed using the
# elements of the given array
 
# Function to find out
# maximum perimeter
def maxPerimeter(arr):
    maxi = 0
    n = len(arr)
     
    # pick up 3 different
    # elements from the array.
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            for k in range(j + 1, n):
                 
                # a, b, c are 3 sides
                # of the triangle
                a = arr[i]
                b = arr[j]
                c = arr[k]
                if(a < b + c and b < a + c
                             and c < a + b):
                    maxi = max(maxi, a + b + c)
 
    if(maxi == 0):
        return "Triangle formation is not possible"
    else:
        return "Maximum Perimeter is: "+ str(maxi)
 
# Driver code
def main():
    arr1 = [6, 1, 6, 5, 8, 4]
    a = maxPerimeter(arr1)
    print(a)
 
    arr2 = [2, 20, 7, 55,
            1, 33, 12, 4]
    a = maxPerimeter(arr2)
    print(a)
 
    arr3 = [33, 6, 20, 1, 8,
            12, 5, 55, 4, 9]
    a = maxPerimeter(arr3)
    print(a)
 
if __name__=='__main__':
    main()
 
# This code is contributed
# by Pritha Updhayay


C#
// Brute force solution to find out
// maximum perimeter triangle which
// can be formed using the elements
// of the given array
using System;
 
class GFG
{
 
    // Function to find out
    // maximum perimeter
    static void maxPerimeter(int []arr,    
                             int n)
    {
     
        // initialize maximum
        // perimeter as 0.
        int maxi = 0;
     
        // pick up 3 different elements
        // from the array.
        for (int i = 0; i < n - 2; i++)
        {
            for (int j = i + 1; j < n - 1; j++)
            {
                for (int k = j + 1; k < n; k++)
                {
     
                    // a, b, c are 3 sides of
                    // the triangle
                    int a = arr[i];
                    int b = arr[j];
                    int c = arr[k];
     
                    // check whether a, b, c
                    // forms a triangle or not.
                    if (a < b + c &&
                        b < c + a &&
                        c < a + b)
                    {
     
                        // if it forms a triangle
                        // then update the maximum
                        // value.
                        maxi = Math.Max(maxi, a + b + c);
                    }
                }
            }
        }
     
        // If maximum perimeter is
        // non-zero then print it.
        if (maxi > 0)
        Console.WriteLine("Maximum Perimeter is: "+ maxi);
     
        // otherwise no triangle
        // formation is possible.
        else
        Console.WriteLine("Triangle formation "+
                          "is not possible.");
    }
     
    // Driver Code
    public static void Main ()
    {
         
        // test case 1
        int []arr1 = {6, 1, 6,
                      5, 8, 4};
        maxPerimeter(arr1, 6);
     
        // test case 2
        int []arr2 = {2, 20, 7, 55,
                      1, 33, 12, 4};
        maxPerimeter(arr2, 8);
     
        // test case 3
        int []arr3 = {33, 6, 20, 1, 8,
                      12, 5, 55, 4, 9};
        maxPerimeter(arr3, 10);
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
// Efficient solution to find
// out maximum perimeter triangle which
// can be formed using the elements
// of the given array
#include 
#include 
 
using namespace std;
 
// Function to find out maximum perimeter
void maxPerimeter(int arr[], int n){
 
    // sort the array elements
    // in reversed order
    sort(arr, arr+n, greater());
 
    // initialize maximum
    // perimeter to 0
    int maxi = 0;
 
    // loop through the sorted array
    // and check whether it forms a
    // triangle or not.
    for (int i = 0; i < n-2; i++){
 
        // Check whether arr[i], arr[i+1]
        // and arr[i+2] forms a triangle
        // or not.
        if (arr[i] < arr[i+1] + arr[i+2]){
 
            // if it forms a triangle then
            // it is the triangle with
            // maximum perimeter.
            maxi = max(maxi, arr[i] + arr[i+1] + arr[i+2]);
            break;
        }
    }
 
    // If maximum perimeter is non-zero
    // then print it.
    if (maxi)
        cout << "Maximum Perimeter is: "
        << maxi << endl;
 
    // otherwise no triangle formation
    // is possible.
    else
        cout << "Triangle formation"
        << "is not possible." << endl;
}
 
// Driver Program
int main()
{
    // test case 1
    int arr1[6] = {6, 1, 6, 5, 8, 4};
    maxPerimeter(arr1, 6);
 
    // test case 2
    int arr2[8] = {2, 20, 7, 55, 1,
                    33, 12, 4};
    maxPerimeter(arr2, 8);
 
    // test case 3
    int arr3[10] = {33, 6, 20, 1, 8,
                    12, 5, 55, 4, 9};
    maxPerimeter(arr3, 10);
 
    return 0;
}


Java
// Efficient solution to find
// out maximum perimeter triangle which
// can be formed using the elements
// of the given array
 
import java.util.Arrays;
 
class GFG {
 
// Function to find out maximum perimeter
    static void maxPerimeter(int arr[], int n) {
 
        // sort the array elements
        // in reversed order
        arr = arrRevSort(arr);
        //sort(arr, arr+n, greater());
 
        // initialize maximum
        // perimeter to 0
        int maxi = 0;
 
        // loop through the sorted array
        // and check whether it forms a
        // triangle or not.
        for (int i = 0; i < n - 2; i++) {
 
            // Check whether arr[i], arr[i+1]
            // and arr[i+2] forms a triangle
            // or not.
            if (arr[i] < arr[i + 1] + arr[i + 2]) {
 
                // if it forms a triangle then
                // it is the triangle with
                // maximum perimeter.
                maxi = Math.max(maxi, arr[i] + arr[i + 1] + arr[i + 2]);
                break;
            }
        }
 
        // If maximum perimeter is non-zero
        // then print it.
        if (maxi > 0) {
            System.out.println("Maximum Perimeter is: " + maxi);
        } // otherwise no triangle formation
        // is possible.
        else {
            System.out.println("Triangle formation is not possible.");
        }
    }
    //Function return sorted array in Decreasing
 
    static int[] arrRevSort(int[] arr) {
        Arrays.sort(arr, 0, arr.length);
        int j = arr.length - 1;
        for (int i = 0; i < arr.length / 2; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        return arr;
    }
 
// Driver Program
    public static void main(String[] args) {
        // test case 1
        int arr1[] = {6, 1, 6, 5, 8, 4};
        maxPerimeter(arr1, 6);
 
        // test case 2
        int arr2[] = {2, 20, 7, 55, 1, 33, 12, 4};
        maxPerimeter(arr2, 8);
 
        // test case 3
        int arr3[] = {33, 6, 20, 1, 8, 12, 5, 55, 4, 9};
        maxPerimeter(arr3, 10);
    }
}
/*This Java code is contributed by 29AjayKumar*/


Python3
# Efficient solution to find
# out maximum perimeter triangle which
# can be formed using the elements
# of the given array
 
# Function to find the
# maximum perimeter
def maxPerimeter(arr):
    maxi = 0
    n = len(arr)
    arr.sort(reverse = True)
 
    for i in range(0, n - 2):
        if arr[i] < (arr[i + 1] + arr[i + 2]):
            maxi = max(maxi, arr[i] +
                       arr[i + 1] + arr[i + 2])
            break
 
    if(maxi == 0):
        return "Triangle formation is not possible"
    else:
        return "Maximum Perimeter is: "+ str(maxi)
 
# Driver Code
def main():
    arr1 = [6, 1, 6, 5, 8, 4]
    a = maxPerimeter(arr1)
    print(a)
 
    arr2 = [2, 20, 7, 55,
            1, 33, 12, 4]
    a = maxPerimeter(arr2)
    print(a)
 
    arr3 = [33, 6, 20, 1, 8,
            12, 5, 55, 4, 9]
    a = maxPerimeter(arr3)
    print(a)
 
if __name__=='__main__':
    main()
 
# This code is contributed
# by Pritha Upadhyay


C#
// Efficient solution to find
// out maximum perimeter triangle which
// can be formed using the elements
// of the given array
 
using System;
 
class GFG {
 
// Function to find out maximum perimeter
    static void maxPerimeter(int[] arr, int n) {
 
        // sort the array elements
        // in reversed order
        arr = arrRevSort(arr);
        //sort(arr, arr+n, greater());
 
        // initialize maximum
        // perimeter to 0
        int maxi = 0;
 
        // loop through the sorted array
        // and check whether it forms a
        // triangle or not.
        for (int i = 0; i < n - 2; i++) {
 
            // Check whether arr[i], arr[i+1]
            // and arr[i+2] forms a triangle
            // or not.
            if (arr[i] < arr[i + 1] + arr[i + 2]) {
 
                // if it forms a triangle then
                // it is the triangle with
                // maximum perimeter.
                maxi = Math.Max(maxi, arr[i] + arr[i + 1] + arr[i + 2]);
                break;
            }
        }
 
        // If maximum perimeter is non-zero
        // then print it.
        if (maxi > 0) {
            Console.WriteLine("Maximum Perimeter is: " + maxi);
        } // otherwise no triangle formation
        // is possible.
        else {
            Console.WriteLine("Triangle formation is not possible.");
        }
    }
    //Function return sorted array in Decreasing
 
    static int[] arrRevSort(int[] arr) {
        Array.Sort(arr);
        int j = arr.Length - 1;
        for (int i = 0; i < arr.Length / 2; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        return arr;
    }
 
// Driver Program
    public static void Main() {
        // test case 1
        int[] arr1 = {6, 1, 6, 5, 8, 4};
        maxPerimeter(arr1, 6);
 
        // test case 2
        int[] arr2 = {2, 20, 7, 55, 1, 33, 12, 4};
        maxPerimeter(arr2, 8);
 
        // test case 3
        int[] arr3 = {33, 6, 20, 1, 8, 12, 5, 55, 4, 9};
        maxPerimeter(arr3, 10);
    }
}
/*This Java code is contributed by mits*/


PHP


输出 :

Maximum Perimeter is: 20
Triangle formation is not possible.
Maximum Perimeter is: 41

高效方法:
首先,我们可以以非递增顺序对数组进行排序。因此,第一个元素将是最大,而最后一个元素将是最小。现在,如果此排序数组的前3个元素形成一个三角形,则它将是最大周长三角形,对于所有其他组合,元素的总和(即该三角形的周长)将为= b> = c)。 a,b,c无法形成三角形,因此a> = b + c。 As,b和c = c + d(如果我们丢掉b取d)或a> = b + d(如果我们丢掉c取d)。因此,我们必须丢掉a并拿起d。
再次对b,c和d进行相同的分析。我们可以继续进行到最后,只要我们找到一个形成三重三角形的三角形,就可以停止检查,因为这个三重三角形提供了最大的周长。
因此,如果排序数组中的arr [i] 以下是此概念的简单实现:

C++

// Efficient solution to find
// out maximum perimeter triangle which
// can be formed using the elements
// of the given array
#include 
#include 
 
using namespace std;
 
// Function to find out maximum perimeter
void maxPerimeter(int arr[], int n){
 
    // sort the array elements
    // in reversed order
    sort(arr, arr+n, greater());
 
    // initialize maximum
    // perimeter to 0
    int maxi = 0;
 
    // loop through the sorted array
    // and check whether it forms a
    // triangle or not.
    for (int i = 0; i < n-2; i++){
 
        // Check whether arr[i], arr[i+1]
        // and arr[i+2] forms a triangle
        // or not.
        if (arr[i] < arr[i+1] + arr[i+2]){
 
            // if it forms a triangle then
            // it is the triangle with
            // maximum perimeter.
            maxi = max(maxi, arr[i] + arr[i+1] + arr[i+2]);
            break;
        }
    }
 
    // If maximum perimeter is non-zero
    // then print it.
    if (maxi)
        cout << "Maximum Perimeter is: "
        << maxi << endl;
 
    // otherwise no triangle formation
    // is possible.
    else
        cout << "Triangle formation"
        << "is not possible." << endl;
}
 
// Driver Program
int main()
{
    // test case 1
    int arr1[6] = {6, 1, 6, 5, 8, 4};
    maxPerimeter(arr1, 6);
 
    // test case 2
    int arr2[8] = {2, 20, 7, 55, 1,
                    33, 12, 4};
    maxPerimeter(arr2, 8);
 
    // test case 3
    int arr3[10] = {33, 6, 20, 1, 8,
                    12, 5, 55, 4, 9};
    maxPerimeter(arr3, 10);
 
    return 0;
}

Java

// Efficient solution to find
// out maximum perimeter triangle which
// can be formed using the elements
// of the given array
 
import java.util.Arrays;
 
class GFG {
 
// Function to find out maximum perimeter
    static void maxPerimeter(int arr[], int n) {
 
        // sort the array elements
        // in reversed order
        arr = arrRevSort(arr);
        //sort(arr, arr+n, greater());
 
        // initialize maximum
        // perimeter to 0
        int maxi = 0;
 
        // loop through the sorted array
        // and check whether it forms a
        // triangle or not.
        for (int i = 0; i < n - 2; i++) {
 
            // Check whether arr[i], arr[i+1]
            // and arr[i+2] forms a triangle
            // or not.
            if (arr[i] < arr[i + 1] + arr[i + 2]) {
 
                // if it forms a triangle then
                // it is the triangle with
                // maximum perimeter.
                maxi = Math.max(maxi, arr[i] + arr[i + 1] + arr[i + 2]);
                break;
            }
        }
 
        // If maximum perimeter is non-zero
        // then print it.
        if (maxi > 0) {
            System.out.println("Maximum Perimeter is: " + maxi);
        } // otherwise no triangle formation
        // is possible.
        else {
            System.out.println("Triangle formation is not possible.");
        }
    }
    //Function return sorted array in Decreasing
 
    static int[] arrRevSort(int[] arr) {
        Arrays.sort(arr, 0, arr.length);
        int j = arr.length - 1;
        for (int i = 0; i < arr.length / 2; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        return arr;
    }
 
// Driver Program
    public static void main(String[] args) {
        // test case 1
        int arr1[] = {6, 1, 6, 5, 8, 4};
        maxPerimeter(arr1, 6);
 
        // test case 2
        int arr2[] = {2, 20, 7, 55, 1, 33, 12, 4};
        maxPerimeter(arr2, 8);
 
        // test case 3
        int arr3[] = {33, 6, 20, 1, 8, 12, 5, 55, 4, 9};
        maxPerimeter(arr3, 10);
    }
}
/*This Java code is contributed by 29AjayKumar*/

Python3

# Efficient solution to find
# out maximum perimeter triangle which
# can be formed using the elements
# of the given array
 
# Function to find the
# maximum perimeter
def maxPerimeter(arr):
    maxi = 0
    n = len(arr)
    arr.sort(reverse = True)
 
    for i in range(0, n - 2):
        if arr[i] < (arr[i + 1] + arr[i + 2]):
            maxi = max(maxi, arr[i] +
                       arr[i + 1] + arr[i + 2])
            break
 
    if(maxi == 0):
        return "Triangle formation is not possible"
    else:
        return "Maximum Perimeter is: "+ str(maxi)
 
# Driver Code
def main():
    arr1 = [6, 1, 6, 5, 8, 4]
    a = maxPerimeter(arr1)
    print(a)
 
    arr2 = [2, 20, 7, 55,
            1, 33, 12, 4]
    a = maxPerimeter(arr2)
    print(a)
 
    arr3 = [33, 6, 20, 1, 8,
            12, 5, 55, 4, 9]
    a = maxPerimeter(arr3)
    print(a)
 
if __name__=='__main__':
    main()
 
# This code is contributed
# by Pritha Upadhyay

C#

// Efficient solution to find
// out maximum perimeter triangle which
// can be formed using the elements
// of the given array
 
using System;
 
class GFG {
 
// Function to find out maximum perimeter
    static void maxPerimeter(int[] arr, int n) {
 
        // sort the array elements
        // in reversed order
        arr = arrRevSort(arr);
        //sort(arr, arr+n, greater());
 
        // initialize maximum
        // perimeter to 0
        int maxi = 0;
 
        // loop through the sorted array
        // and check whether it forms a
        // triangle or not.
        for (int i = 0; i < n - 2; i++) {
 
            // Check whether arr[i], arr[i+1]
            // and arr[i+2] forms a triangle
            // or not.
            if (arr[i] < arr[i + 1] + arr[i + 2]) {
 
                // if it forms a triangle then
                // it is the triangle with
                // maximum perimeter.
                maxi = Math.Max(maxi, arr[i] + arr[i + 1] + arr[i + 2]);
                break;
            }
        }
 
        // If maximum perimeter is non-zero
        // then print it.
        if (maxi > 0) {
            Console.WriteLine("Maximum Perimeter is: " + maxi);
        } // otherwise no triangle formation
        // is possible.
        else {
            Console.WriteLine("Triangle formation is not possible.");
        }
    }
    //Function return sorted array in Decreasing
 
    static int[] arrRevSort(int[] arr) {
        Array.Sort(arr);
        int j = arr.Length - 1;
        for (int i = 0; i < arr.Length / 2; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        return arr;
    }
 
// Driver Program
    public static void Main() {
        // test case 1
        int[] arr1 = {6, 1, 6, 5, 8, 4};
        maxPerimeter(arr1, 6);
 
        // test case 2
        int[] arr2 = {2, 20, 7, 55, 1, 33, 12, 4};
        maxPerimeter(arr2, 8);
 
        // test case 3
        int[] arr3 = {33, 6, 20, 1, 8, 12, 5, 55, 4, 9};
        maxPerimeter(arr3, 10);
    }
}
/*This Java code is contributed by mits*/

的PHP


输出 :

Maximum Perimeter is: 20
Triangle formation is not possible.
Maximum Perimeter is: 41

该方法的时间复杂度为O(n * log(n))。对数组进行排序需要大量时间。