📜  通过从阵列中选取四个边的最大面积矩形

📅  最后修改于: 2021-10-28 01:27:13             🧑  作者: Mango

给定一个由 n 个表示长度的正整数组成的数组。找出从给定数组中选取四个边的最大可能区域。请注意,只有在给定数组中有两对相等的值时才能形成矩形。

例子:

Input : arr[] = {2, 1, 2, 5, 4, 4}
Output : 8
Explanation : Dimension will be 4 * 2

Input : arr[] = {2, 1, 3, 5, 4, 4}
Output : 0
Explanation : No rectangle possible

方法一(排序)
该任务基本上简化为在数组中找到两对相等的值。如果超过两对,则选择具有最大值的两对。一个简单的解决方案是执行以下操作。
1) 对给定的数组进行排序。
2) 从最大到最小值遍历数组并返回具有最大值的两对。

C++
// CPP program for finding maximum area possible
// of a rectangle
#include 
using namespace std;
 
// function for finding max area
int findArea(int arr[], int n)
{
    // sort array in non-increasing order
    sort(arr, arr + n, greater());
 
    // Initialize two sides of rectangle
    int dimension[2] = { 0, 0 };
 
    // traverse through array
    for (int i = 0, j = 0; i < n - 1 && j < 2; i++)
 
        // if any element occurs twice
        // store that as dimension
        if (arr[i] == arr[i + 1])
            dimension[j++] = arr[i++];
 
    // return the product of dimensions
    return (dimension[0] * dimension[1]);
}
 
// driver function
int main()
{
    int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findArea(arr, n);
    return 0;
}


Java
// Java program for finding maximum area
// possible of a rectangle
import java.util.Arrays;
import java.util.Collections;
 
public class GFG
{    
    // function for finding max area
    static int findArea(Integer arr[], int n)
    {
        // sort array in non-increasing order
        Arrays.sort(arr, Collections.reverseOrder());
      
        // Initialize two sides of rectangle
        int[] dimension = { 0, 0 };
      
        // traverse through array
        for (int i = 0, j = 0; i < n - 1 && j < 2;
                                           i++)
      
            // if any element occurs twice
            // store that as dimension
            if (arr[i] == arr[i + 1])
                dimension[j++] = arr[i++];
      
        // return the product of dimensions
        return (dimension[0] * dimension[1]);
    }
      
    // driver function
    public static void main(String args[])
    {
        Integer arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
        int n = arr.length;
        System.out.println(findArea(arr, n));
    }
}
// This code is contributed by Sumit Ghosh


Python3
# Python3 program for finding
# maximum area possible of
# a rectangle
 
# function for finding
# max area
def findArea(arr, n):
 
    # sort array in
    # non-increasing order
    arr.sort(reverse = True)
 
    # Initialize two
    # sides of rectangle
    dimension = [0, 0]
 
    # traverse through array
    i = 0
    j = 0
    while(i < n - 1 and j < 2):
 
        # if any element occurs twice
        # store that as dimension
        if (arr[i] == arr[i + 1]):
            dimension[j] = arr[i]
            j += 1
            i += 1
        i += 1
         
    # return the product
    # of dimensions
    return (dimension[0] *
            dimension[1])
 
# Driver code
arr = [4, 2, 1, 4, 6, 6, 2, 5]
n = len(arr)
print(findArea(arr, n))
 
# This code is contributed
# by Smitha


C#
// C# program for finding maximum area
// possible of a rectangle
using System;
using System.Collections;
 
class GFG
{
// function for finding max area
static int findArea(int []arr, int n)
{
    // sort array in non-increasing order
    Array.Sort(arr);
    Array.Reverse(arr);
 
    // Initialize two sides of rectangle
    int[] dimension = { 0, 0 };
 
    // traverse through array
    for (int i = 0, j = 0;
             i < n - 1 && j < 2; i++)
 
        // if any element occurs twice
        // store that as dimension
        if (arr[i] == arr[i + 1])
            dimension[j++] = arr[i++];
 
    // return the product of dimensions
    return (dimension[0] * dimension[1]);
}
 
// Driver Code
public static void Main(String []args)
{
    int []arr = { 4, 2, 1, 4, 6, 6, 2, 5 };
    int n = arr.Length;
    Console.Write(findArea(arr, n));
}
}
 
// This code is contributed
// by PrinciRaj1992


PHP


Javascript


C++
// CPP program for finding maximum area possible
// of a rectangle
#include 
using namespace std;
 
// function for finding max area
int findArea(int arr[], int n)
{
    unordered_set s;
 
    // traverse through array
    int first = 0, second = 0;
    for (int i = 0; i < n; i++) {
 
        // If this is first occurrence of arr[i],
        // simply insert and continue
        if (s.find(arr[i]) == s.end()) {
            s.insert(arr[i]);
            continue;
        }
 
        // If this is second (or more) occurrence,
        // update first and second maximum values.
        if (arr[i] > first) {
            second = first;
            first = arr[i];
        } else if (arr[i] > second)
            second = arr[i];
    }
 
    // return the product of dimensions
    return (first * second);
}
 
// driver function
int main()
{
    int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findArea(arr, n);
    return 0;
}


Java
// Java program for finding maximum
// area possible of a rectangle
import java.util.HashSet;
import java.util.Set;
 
public class GFG
{    
    // function for finding max area
    static int findArea(int arr[], int n)
    {
        //unordered_set s;
         
        Set s = new HashSet<>();
      
        // traverse through array
        int first = 0, second = 0;
        for (int i = 0; i < n; i++) {
      
            // If this is first occurrence of
            // arr[i], simply insert and continue
            if (!s.contains(arr[i])) {
                s.add(arr[i]);
                continue;
            }
      
            // If this is second (or more)
            // occurrence, update first and
            // second maximum values.
            if (arr[i] > first) {
                second = first;
                first = arr[i];
            } else if (arr[i] > second)
                second = arr[i];
        }
      
        // return the product of dimensions
        return (first * second);
    }
      
    // driver function
    public static void main(String args[])
    {
        int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
        int n = arr.length;
        System.out.println(findArea(arr, n));
    }
}
// This code is contributed by Sumit Ghosh


Python3
# Python 3 program for finding maximum
# area possible of a rectangle
 
# function for finding max area
def findArea(arr, n):
 
    s = []
 
    # traverse through array
    first = 0
    second = 0
    for i in range(n) :
 
        # If this is first occurrence of
        # arr[i], simply insert and continue
        if arr[i] not in s:
            s.append(arr[i])
            continue
 
        # If this is second (or more) occurrence,
        # update first and second maximum values.
        if (arr[i] > first) :
            second = first
            first = arr[i]
        elif (arr[i] > second):
            second = arr[i]
 
    # return the product of dimensions
    return (first * second)
 
# Driver Code
if __name__ == "__main__":
     
    arr = [ 4, 2, 1, 4, 6, 6, 2, 5 ]
    n = len(arr)
    print(findArea(arr, n))
 
# This code is contributed by ita_c


C#
using System;
using System.Collections.Generic;
 
// c# program for finding maximum 
// area possible of a rectangle
 
public class GFG
{
    // function for finding max area
    public static int findArea(int[] arr, int n)
    {
        //unordered_set s;
 
        ISet s = new HashSet();
 
        // traverse through array
        int first = 0, second = 0;
        for (int i = 0; i < n; i++)
        {
 
            // If this is first occurrence of 
            // arr[i], simply insert and continue
            if (!s.Contains(arr[i]))
            {
                s.Add(arr[i]);
                continue;
            }
 
            // If this is second (or more) 
            // occurrence, update first and 
            // second maximum values.
            if (arr[i] > first)
            {
                second = first;
                first = arr[i];
            }
            else if (arr[i] > second)
            {
                second = arr[i];
            }
        }
 
        // return the product of dimensions
        return (first * second);
    }
 
    // driver function
    public static void Main(string[] args)
    {
        int[] arr = new int[] {4, 2, 1, 4, 6, 6, 2, 5};
        int n = arr.Length;
        Console.WriteLine(findArea(arr, n));
    }
}
 
// This code is contributed by Shrikant13


Javascript


输出:

24

时间复杂度:O(n Log n)方法二(散列)
这个想法是在哈希集中插入所有第一次出现的元素。对于第二次出现,跟踪最多两个值。

C++

// CPP program for finding maximum area possible
// of a rectangle
#include 
using namespace std;
 
// function for finding max area
int findArea(int arr[], int n)
{
    unordered_set s;
 
    // traverse through array
    int first = 0, second = 0;
    for (int i = 0; i < n; i++) {
 
        // If this is first occurrence of arr[i],
        // simply insert and continue
        if (s.find(arr[i]) == s.end()) {
            s.insert(arr[i]);
            continue;
        }
 
        // If this is second (or more) occurrence,
        // update first and second maximum values.
        if (arr[i] > first) {
            second = first;
            first = arr[i];
        } else if (arr[i] > second)
            second = arr[i];
    }
 
    // return the product of dimensions
    return (first * second);
}
 
// driver function
int main()
{
    int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findArea(arr, n);
    return 0;
}

Java

// Java program for finding maximum
// area possible of a rectangle
import java.util.HashSet;
import java.util.Set;
 
public class GFG
{    
    // function for finding max area
    static int findArea(int arr[], int n)
    {
        //unordered_set s;
         
        Set s = new HashSet<>();
      
        // traverse through array
        int first = 0, second = 0;
        for (int i = 0; i < n; i++) {
      
            // If this is first occurrence of
            // arr[i], simply insert and continue
            if (!s.contains(arr[i])) {
                s.add(arr[i]);
                continue;
            }
      
            // If this is second (or more)
            // occurrence, update first and
            // second maximum values.
            if (arr[i] > first) {
                second = first;
                first = arr[i];
            } else if (arr[i] > second)
                second = arr[i];
        }
      
        // return the product of dimensions
        return (first * second);
    }
      
    // driver function
    public static void main(String args[])
    {
        int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
        int n = arr.length;
        System.out.println(findArea(arr, n));
    }
}
// This code is contributed by Sumit Ghosh

蟒蛇3

# Python 3 program for finding maximum
# area possible of a rectangle
 
# function for finding max area
def findArea(arr, n):
 
    s = []
 
    # traverse through array
    first = 0
    second = 0
    for i in range(n) :
 
        # If this is first occurrence of
        # arr[i], simply insert and continue
        if arr[i] not in s:
            s.append(arr[i])
            continue
 
        # If this is second (or more) occurrence,
        # update first and second maximum values.
        if (arr[i] > first) :
            second = first
            first = arr[i]
        elif (arr[i] > second):
            second = arr[i]
 
    # return the product of dimensions
    return (first * second)
 
# Driver Code
if __name__ == "__main__":
     
    arr = [ 4, 2, 1, 4, 6, 6, 2, 5 ]
    n = len(arr)
    print(findArea(arr, n))
 
# This code is contributed by ita_c

C#

using System;
using System.Collections.Generic;
 
// c# program for finding maximum 
// area possible of a rectangle
 
public class GFG
{
    // function for finding max area
    public static int findArea(int[] arr, int n)
    {
        //unordered_set s;
 
        ISet s = new HashSet();
 
        // traverse through array
        int first = 0, second = 0;
        for (int i = 0; i < n; i++)
        {
 
            // If this is first occurrence of 
            // arr[i], simply insert and continue
            if (!s.Contains(arr[i]))
            {
                s.Add(arr[i]);
                continue;
            }
 
            // If this is second (or more) 
            // occurrence, update first and 
            // second maximum values.
            if (arr[i] > first)
            {
                second = first;
                first = arr[i];
            }
            else if (arr[i] > second)
            {
                second = arr[i];
            }
        }
 
        // return the product of dimensions
        return (first * second);
    }
 
    // driver function
    public static void Main(string[] args)
    {
        int[] arr = new int[] {4, 2, 1, 4, 6, 6, 2, 5};
        int n = arr.Length;
        Console.WriteLine(findArea(arr, n));
    }
}
 
// This code is contributed by Shrikant13

Javascript


输出:

24

时间复杂度: O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程