Largest area square in an array when elements can be shuffled
Last Updated :
08 Mar, 2022
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++
#include <bits/stdc++.h>
using namespace std;
bool isSquarePossible( int arr[], int n, int l)
{
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] >= l)
cnt++;
if (cnt >= l)
return true ;
}
return false ;
}
int maxArea( int arr[], int n)
{
int l = 0, r = n;
int len = 0;
while (l <= r) {
int m = l + ((r - l) / 2);
if (isSquarePossible(arr, n, m)) {
len = m;
l = m + 1;
}
else
r = m - 1;
}
return (len * len);
}
int main()
{
int arr[] = { 1, 3, 4, 5, 5 };
int n = sizeof (arr) / sizeof ( int );
cout << maxArea(arr, n);
return 0;
}
|
Java
class GFG
{
static boolean isSquarePossible( int arr[],
int n, int l)
{
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] >= l)
cnt++;
if (cnt >= l)
return true ;
}
return false ;
}
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 (isSquarePossible(arr, n, m))
{
len = m;
l = m + 1 ;
}
else
r = m - 1 ;
}
return (len * len);
}
public static void main (String[] args)
{
int arr[] = { 1 , 3 , 4 , 5 , 5 };
int n = arr.length;
System.out.println(maxArea(arr, n));
}
}
|
Python3
def isSquarePossible(arr, n, l) :
cnt = 0
for i in range (n) :
if arr[i] > = l :
cnt + = 1
if cnt > = l :
return True
return False
def maxArea(arr, n) :
l , r = 0 , n
len = 0
while l < = r :
m = l + ((r - l) / / 2 )
if isSquarePossible(arr, n, m) :
len = m
l = m + 1
else :
r = m - 1
return ( len * len )
arr = [ 1 , 3 , 4 , 5 , 5 ]
n = len (arr)
print (maxArea(arr, n))
|
C#
using System;
class GFG
{
static bool isSquarePossible( int []arr,
int n, int l)
{
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] >= l)
cnt++;
if (cnt >= l)
return true ;
}
return false ;
}
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 (isSquarePossible(arr, n, m))
{
len = m;
l = m + 1;
}
else
r = m - 1;
}
return (len * len);
}
public static void Main()
{
int []arr = { 1, 3, 4, 5, 5 };
int n = arr.Length;
Console.WriteLine(maxArea(arr, n));
}
}
|
Javascript
<script>
function isSquarePossible(arr, n, l) {
let cnt = 0;
for (let i = 0; i < n; i++) {
if (arr[i] >= l)
cnt++;
if (cnt >= l)
return true ;
}
return false ;
}
function maxArea(arr, n) {
let l = 0, r = n;
let len = 0;
while (l <= r) {
let m = l + Math.floor((r - l) / 2);
if (isSquarePossible(arr, n, m)) {
len = m;
l = m + 1;
}
else
r = m - 1;
}
return (len * len);
}
let arr = [1, 3, 4, 5, 5];
let n = arr.length;
document.write(maxArea(arr, n));
</script>
|
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...