Area of the largest square that can be formed from the given length sticks using Hashing
Last Updated :
06 Apr, 2021
Given an array arr[] of N integers representing the heights of the sticks. The task is to find the area of the largest square that can be formed using these sticks and the count of such squares. Note that a single side of the square can only use a single stick.
Examples:
Input: arr[] = {5, 3, 2, 3, 6, 3, 3}
Output:
Area = 9
Count = 1
Side of the square will be 3 and
only one such square is possible.
Input: arr[] = {2, 2, 2, 9, 2, 2, 2, 2, 2}
Output:
Area = 4
Count = 2
Approach: Count the frequencies of all the elements of the array. Now, starting from the maximum (in order to maximize the area) find the first frequency which is at least 4 so that a square can be formed then the area can be calculated as freq[i] * freq[i] and the count of such squares will be freq[i] / 4.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMaxSquare( int arr[], int n)
{
int maxVal = *max_element(arr, arr + n);
int freq[maxVal + 1] = { 0 };
for ( int i = 0; i < n; i++)
freq[arr[i]]++;
for ( int i = maxVal; i > 0; i--) {
if (freq[i] >= 4) {
cout << "Area = " << ( pow (i, 2));
cout << "\nCount = " << (freq[i] / 4);
return ;
}
}
cout << "-1" ;
}
int main()
{
int arr[] = { 2, 2, 2, 9, 2, 2, 2, 2, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
findMaxSquare(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findMaxSquare( int arr[], int n)
{
int maxVal = Arrays.stream(arr).max().getAsInt();
int []freq = new int [maxVal + 1 ];
for ( int i = 0 ; i < n; i++)
freq[arr[i]]++;
for ( int i = maxVal; i > 0 ; i--)
{
if (freq[i] >= 4 )
{
System.out.print( "Area = " +
(Math.pow(i, 2 )));
System.out.print( "\nCount = " +
(freq[i] / 4 ));
return ;
}
}
System.out.print( "-1" );
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 2 , 9 , 2 , 2 , 2 , 2 , 2 };
int n = arr.length;
findMaxSquare(arr, n);
}
}
|
Python3
def findMaxSquare(arr, n) :
maxVal = max (arr);
freq = [ 0 ] * (maxVal + 1 ) ;
for i in range (n) :
freq[arr[i]] + = 1 ;
for i in range (maxVal, 0 , - 1 ) :
if (freq[i] > = 4 ) :
print ( "Area = " , pow (i, 2 ));
print ( "Count =" , freq[i] / / 4 );
return ;
print ( "-1" );
if __name__ = = "__main__" :
arr = [ 2 , 2 , 2 , 9 , 2 , 2 , 2 , 2 , 2 ];
n = len (arr);
findMaxSquare(arr, n);
|
C#
using System;
using System.Linq;
class GFG
{
static void findMaxSquare( int []arr, int n)
{
int maxVal = arr.Max();
int []freq = new int [maxVal + 1];
for ( int i = 0; i < n; i++)
freq[arr[i]]++;
for ( int i = maxVal; i > 0; i--)
{
if (freq[i] >= 4)
{
Console.Write( "Area = " +
(Math.Pow(i, 2)));
Console.Write( "\nCount = " +
(freq[i] / 4));
return ;
}
}
Console.Write( "-1" );
}
public static void Main(String[] args)
{
int []arr = { 2, 2, 2, 9, 2, 2, 2, 2, 2 };
int n = arr.Length;
findMaxSquare(arr, n);
}
}
|
Javascript
<script>
function findMaxSquare(arr, n)
{
var maxVal = Math.max(...arr);
var freq = Array(maxVal + 1).fill(0);
for ( var i = 0; i < n; i++)
freq[arr[i]]++;
for ( var i = maxVal; i > 0; i--) {
if (freq[i] >= 4) {
document.write( "Area = " + (Math.pow(i, 2)));
document.write( "<br>Count = " + (freq[i] / 4));
return ;
}
}
document.write( "-1" );
}
var arr = [ 2, 2, 2, 9, 2, 2, 2, 2, 2 ];
var n = arr.length;
findMaxSquare(arr, n);
</script>
|
Output:
Area = 4
Count = 2
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...