📌  相关文章
📜  元素可以混洗时数组中面积最大的正方形

📅  最后修改于: 2021-04-24 03:58:19             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,其中arr [i]是第i巧克力的高度,并且所有已涂巧克力的巧克力的宽度均为1个单位,则任务是在巧克力可以以任何顺序排列。

例子:

的方法:边的正方形,如果存在至少应阵列是等于或大于a元件可制得。二进制搜索可用于查找可以在0N范围内实现的正方形的最大边。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if it
// is possible to make a square
// with side equal to l
bool isSquarePossible(int arr[], int n, int l)
{
  
    // To store the count of elements
    // greater than or equal to l
    int cnt = 0;
    for (int i = 0; i < n; i++) {
  
        // Increment the count
        if (arr[i] >= l)
            cnt++;
  
        // If the count becomes greater
        // than or equal to l
        if (cnt >= l)
            return true;
    }
  
    return false;
}
  
// Function to return the
// maximum area of the square
// that can be obtained
int maxArea(int arr[], int n)
{
    int l = 0, r = n;
    int len = 0;
    while (l <= r) {
        int m = l + ((r - l) / 2);
  
        // If square is possible with
        // side length m
        if (isSquarePossible(arr, n, m)) {
            len = m;
            l = m + 1;
        }
  
        // Try to find a square with
        // smaller side length
        else
            r = m - 1;
    }
  
    // Return the area
    return (len * len);
}
  
// Driver code
int main()
{
    int arr[] = { 1, 3, 4, 5, 5 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << maxArea(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG
{
      
    // Function that returns true if it 
    // is possible to make a square 
    // with side equal to l 
    static boolean isSquarePossible(int arr[], 
                                    int n, int l) 
    { 
      
        // To store the count of elements 
        // greater than or equal to l 
        int cnt = 0; 
        for (int i = 0; i < n; i++)
        { 
      
            // Increment the count 
            if (arr[i] >= l) 
                cnt++; 
      
            // If the count becomes greater 
            // than or equal to l 
            if (cnt >= l) 
                return true; 
        } 
        return false; 
    } 
      
    // Function to return the 
    // maximum area of the square 
    // that can be obtained 
    static int maxArea(int arr[], int n) 
    { 
        int l = 0, r = n; 
        int len = 0; 
        while (l <= r) 
        { 
            int m = l + ((r - l) / 2); 
      
            // If square is possible with 
            // side length m 
            if (isSquarePossible(arr, n, m))
            { 
                len = m; 
                l = m + 1; 
            } 
      
            // Try to find a square with 
            // smaller side length 
            else
                r = m - 1; 
        } 
      
        // Return the area 
        return (len * len); 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        int arr[] = { 1, 3, 4, 5, 5 }; 
        int n = arr.length; 
      
        System.out.println(maxArea(arr, n)); 
    } 
}
  
// This code is contributed by kanugargng


Python3
# Python3 implementation of the approach 
  
# Function that returns true if it 
# is possible to make a square 
# with side equal to l 
def isSquarePossible(arr, n, l) : 
  
    # To store the count of elements 
    # greater than or equal to l 
    cnt = 0
    for i in range(n) :
  
        # Increment the count 
        if arr[i] >= l : 
            cnt += 1
  
        # If the count becomes greater 
        # than or equal to l 
        if cnt >= l : 
            return True
  
    return False
  
# Function to return the 
# maximum area of the square 
# that can be obtained 
def maxArea(arr, n) : 
  
    l , r = 0, n 
    len = 0
    while l <= r : 
        m = l + ((r - l) // 2) 
  
        # If square is possible with 
        # side length m 
        if isSquarePossible(arr, n, m) : 
            len = m 
            l = m + 1
  
        # Try to find a square with 
        # smaller side length 
        else :
            r = m - 1
  
    # Return the area 
    return (len * len)
  
# Driver code 
arr = [ 1, 3, 4, 5, 5 ] 
n = len(arr)
  
print(maxArea(arr, n)) 
  
# This code is contributed by divyamohan


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
      
    // Function that returns true if it 
    // is possible to make a square 
    // with side equal to l 
    static bool isSquarePossible(int []arr, 
                                 int n, int l) 
    { 
      
        // To store the count of elements 
        // greater than or equal to l 
        int cnt = 0; 
        for (int i = 0; i < n; i++) 
        { 
      
            // Increment the count 
            if (arr[i] >= l) 
                cnt++; 
      
            // If the count becomes greater 
            // than or equal to l 
            if (cnt >= l) 
                return true; 
        } 
        return false; 
    } 
      
    // Function to return the 
    // maximum area of the square 
    // that can be obtained 
    static int maxArea(int []arr, int n) 
    { 
        int l = 0, r = n; 
        int len = 0; 
        while (l <= r) 
        { 
            int m = l + ((r - l) / 2); 
      
            // If square is possible with 
            // side length m 
            if (isSquarePossible(arr, n, m)) 
            { 
                len = m; 
                l = m + 1; 
            } 
      
            // Try to find a square with 
            // smaller side length 
            else
                r = m - 1; 
        } 
      
        // Return the area 
        return (len * len); 
    } 
      
    // Driver code 
    public static void Main() 
    { 
        int []arr = { 1, 3, 4, 5, 5 }; 
        int n = arr.Length; 
      
        Console.WriteLine(maxArea(arr, n)); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
9