Largest area square in an array when elements can be shuffled
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: 9
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 <bits/stdc++.h> 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 |
Javascript
<script> // JavaScript implementation of the approach // Function that returns true if it // is possible to make a square // with side equal to l function isSquarePossible(arr, n, l) { // To store the count of elements // greater than or equal to l let cnt = 0; for (let 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 function maxArea(arr, n) { let l = 0, r = n; let len = 0; while (l <= r) { let m = l + Math.floor((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 let arr = [1, 3, 4, 5, 5]; let n = arr.length; document.write(maxArea(arr, n)); </script> |
9
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Contact Us