📌  相关文章
📜  最大化通过从给定数组中选择边形成的四边形的周长

📅  最后修改于: 2022-05-13 01:56:07.808000             🧑  作者: Mango

最大化通过从给定数组中选择边形成的四边形的周长

给定一个大小为N的数组arr ,其中每个元素表示边的长度,任务是找到可以使用给定数组中的边创建的最大周长四边形。如果不能形成四边形,则打印-1。

例子:

方法:给定问题可以通过迭代给定数组中所有可能的边(a,b,c,d)组合来解决。可以看出,只有当三个较小边之和大于或等于最大边,或者四边形的周长大于或等于(2 * 最大的一面)。因此,遍历给定数组arr[](a, b, c, d)的所有可能值,并检查它是否形成有效的四边形。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find largest perimeter
// of a possible quadrilateral
int largestPerimeterQuad(int* arr, int n)
{
    // Stores the final answer
    int ans = -1;
 
    // Loop to iterate over all possible
    // sides (a, b, c, d) that can be
    // formed from the given array
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int k = j + 1; k < n; k++) {
                for (int l = k + 1; l < n; l++) {
 
                    // If a quadrilateral can
                    // be formed from current
                    // selected sides
                    if (arr[i] + arr[j]
                            + arr[k] + arr[l]
                        >= 2
                               * max(arr[i],
                                     max(arr[j],
                                         max(arr[k],
                                             arr[l]))))
 
                        // Update maximum
                        ans = max(ans, arr[i] + arr[j]
                                           + arr[k]
                                           + arr[l]);
                }
            }
        }
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 1, 2, 4, 2, 1 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << largestPerimeterQuad(arr, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
 
  // Function to find largest perimeter
  // of a possible quadrilateral
  static int largestPerimeterQuad(int []arr, int n)
  {
 
    // Stores the final answer
    int ans = -1;
 
    // Loop to iterate over all possible
    // sides (a, b, c, d) that can be
    // formed from the given array
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        for (int k = j + 1; k < n; k++) {
          for (int l = k + 1; l < n; l++) {
 
            // If a quadrilateral can
            // be formed from current
            // selected sides
            if (arr[i] + arr[j]
                + arr[k] + arr[l]
                >= 2
                * Math.max(arr[i],
                           Math.max(arr[j],
                                    Math.max(arr[k],
                                             arr[l]))))
 
              // Update maximum
              ans = Math.max(ans, arr[i] + arr[j]
                             + arr[k]
                             + arr[l]);
          }
        }
      }
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int []arr = { 3, 1, 2, 4, 2, 1 };
    int n = arr.length;
 
    System.out.print(largestPerimeterQuad(arr, n));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python program for the above approach
 
# Function to find largest perimeter
# of a possible quadrilateral
def largestPerimeterQuad(arr, n):
 
    # Stores the final answer
    ans = -1
 
    # Loop to iterate over all possible
    # sides (a, b, c, d) that can be
    # formed from the given array
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                for l in range(k + 1, n):
                   
                    # If a quadrilateral can
                    # be formed from current
                    # selected sides
                    if (arr[i] + arr[j]
                        + arr[k] + arr[l]
                        >= 2 * max(arr[i],
                                   max(arr[j],
                                       max(arr[k],
                                           arr[l])))):
 
                        # Update maximum
                        ans = max(ans, arr[i] + arr[j] + arr[k] + arr[l])
    # Return Answer
    return ans
 
# Driver Code
arr = [3, 1, 2, 4, 2, 1]
n = len(arr)
 
print(largestPerimeterQuad(arr, n))
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
class GFG
{
   
// Function to find largest perimeter
// of a possible quadrilateral
static int largestPerimeterQuad(int []arr, int n)
{
   
    // Stores the final answer
    int ans = -1;
 
    // Loop to iterate over all possible
    // sides (a, b, c, d) that can be
    // formed from the given array
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int k = j + 1; k < n; k++) {
                for (int l = k + 1; l < n; l++) {
 
                    // If a quadrilateral can
                    // be formed from current
                    // selected sides
                    if (arr[i] + arr[j]
                            + arr[k] + arr[l]
                        >= 2
                               * Math.Max(arr[i],
                                     Math.Max(arr[j],
                                         Math.Max(arr[k],
                                             arr[l]))))
 
                        // Update maximum
                        ans = Math.Max(ans, arr[i] + arr[j]
                                           + arr[k]
                                           + arr[l]);
                }
            }
        }
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 3, 1, 2, 4, 2, 1 };
    int n = arr.Length;
 
    Console.Write(largestPerimeterQuad(arr, n));
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
11

时间复杂度: O(N 4 )
辅助空间: O(1)