Largest area square in an array when elements can be shuffled

• Difficulty Level : Medium
• Last Updated : 03 Jun, 2021

Given an array arr[] of N integers where arr[i] is the height of the ith chocolate and all the chocolates are 1 unit wide, the task is to find the maximum area for any square made from the chocolates when the chocolates can be arranged in any order.
Examples:

Input: arr[] = {1, 3, 4, 5, 5}
Output:
Square with side = 3 can be obtained
from either {3, 4, 5} or {4, 5, 5}.
Input: arr[] = {6, 1, 6, 6, 6}
Output: 16

Approach: A square of side a can be obtained if there exists atleast a element in the array which are either equal to or greater than a. Binary Search can be used to find the maximum side of the square that could be achieved within the range of 0 to N.
Below is the implementation of the above approach:

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 lbool 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 obtainedint 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 codeint 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 approachclass 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 ldef 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 obtaineddef 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 codearr = [ 1, 3, 4, 5, 5 ]n = len(arr) print(maxArea(arr, n)) # This code is contributed by divyamohan

C#

 // C# implementation of the approachusing 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

Javascript


Output:
9

My Personal Notes arrow_drop_up