Open In App

Maximize Perimeter of Quadrilateral formed by choosing sides from given Array

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given an array arr of size N where each element represents the length of a side, the task is to find the maximum perimeter quadrilateral that can be created using the sides in the given array. If no quadrilateral can be formed print -1.

Examples:

Input: arr[ ] = {3, 1, 2, 4, 2, 1}
Output: 11
Explanation: A quadrilateral of perimeter 4 + 3 + 2 + 2 = 11 can be formed which is the maximum possible.

Input: arr[ ] = {1, 1, 2, 2, 20}
Output: 6

Input: arr[ ] = {1, 2, 4, 10}
Output: -1

 

Approach: The given problem can be solved by iterating through all the possible combinations of the sides (a, b, c, d) from the given array. It can be observed that sides a, b, c, and d form a valid quadrilateral only if the sum of three smaller sides is greater than or equal to the largest side, or the perimeter of the quadrilateral is greater than or equal to (2 * largest side). Therefore, iterate through all the possible values of (a, b, c, d) from the given array arr[] and check if it forms a valid quadrilateral. 

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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




<script>
    // JavaScript program for the above approach
 
    // Function to find largest perimeter
    // of a possible quadrilateral
    const largestPerimeterQuad = (arr, n) => {
     
        // Stores the final answer
        let ans = -1;
 
        // Loop to iterate over all possible
        // sides (a, b, c, d) that can be
        // formed from the given array
        for (let i = 0; i < n; i++) {
            for (let j = i + 1; j < n; j++) {
                for (let k = j + 1; k < n; k++) {
                    for (let 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
    let arr = [3, 1, 2, 4, 2, 1];
    let n = arr.length;
 
    document.write(largestPerimeterQuad(arr, n));
 
    // This code is contributed by rakeshsahni
 
</script>


 
 

Output

11

 

Time Complexity: O(N4)
Auxiliary Space: O(1)

 



Last Updated : 03 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads