Largest square which can be formed using given rectangular blocks
Last Updated :
05 Sep, 2023
Given an array arr[] of positive integers where each element of the array represents the length of the rectangular blocks. The task is to find the largest length of the square which can be formed using the rectangular blocks.
Examples:
Input: arr[] = {3, 2, 1, 5, 2, 4}
Output: 3
Explanation:
Using rectangular block of length 3, 5 and 4, square of side length 3 can be constructed as shown below:
Input: arr[] = {1, 2, 3}
Output: 2
Approach:
- Sort the given array in decreasing order.
- Initialise maximum sidelength(say maxLength) as 0.
- Traverse the array arr[] and if arr[i] > maxLength then increment the maxLength and check this condition for next iteration.
- If the above condition doesn’t satisfy then break the loop and print the maxLength.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxSide( int a[], int n)
{
int sideLength = 0;
sort(a, a + n);
for ( int i = n - 1; i >= 0; i--) {
if (a[i] > sideLength) {
sideLength++;
}
else {
break ;
}
}
cout << sideLength << endl;
}
int main()
{
int N = 6;
int arr[] = { 3, 2, 1, 5, 2, 4 };
maxSide(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static void maxSide( int a[], int n)
{
int sideLength = 0 ;
Arrays.sort(a);
for ( int i = n - 1 ; i >= 0 ; i--)
{
if (a[i] > sideLength)
{
sideLength++;
}
else
{
break ;
}
}
System.out.println(sideLength);
}
public static void main (String[] args)
{
int N = 6 ;
int arr[] = new int []{ 3 , 2 , 1 ,
5 , 2 , 4 };
maxSide(arr, N);
}
}
|
Python3
def maxSide(a, n):
sideLength = 0
a.sort
for i in range (n - 1 , - 1 , - 1 ):
if (a[i] > sideLength):
sideLength + = 1
else :
break
print (sideLength)
N = 6
arr = [ 3 , 2 , 1 , 5 , 2 , 4 ]
maxSide(arr, N)
|
C#
using System;
class GFG{
static void maxSide( int []a, int n)
{
int sideLength = 0;
Array.Sort(a);
for ( int i = n - 1; i >= 0; i--)
{
if (a[i] > sideLength)
{
sideLength++;
}
else
{
break ;
}
}
Console.Write(sideLength);
}
public static void Main()
{
int N = 6;
int []arr = new int []{ 3, 2, 1,
5, 2, 4 };
maxSide(arr, N);
}
}
|
Javascript
<script>
function maxSide( a, n) {
let sideLength = 0;
a.sort();
for ( i = n - 1; i >= 0; i--) {
if (a[i] > sideLength) {
sideLength++;
} else {
break ;
}
}
document.write(sideLength);
}
let N = 6;
let arr = [3, 2, 1, 5, 2, 4 ];
maxSide(arr, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Approach: Binary Search
Idea: Start by setting the low and high values as 1 and the sum of all elements in the array, respectively. Then, check if a square of mid-length can be formed using the given blocks. If yes, update the low value as mid+1. Otherwise, update the high value as mid-1.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
bool isSquarePossible(vector< int >& arr, int k)
{
int count = 0;
for ( int i = 0; i < arr.size(); i++) {
count += arr[i] / k;
if (count >= k)
return true ;
}
return false ;
}
int largestSquareLength(vector< int >& arr)
{
int n = arr.size();
int low = 1;
int high = 0;
for ( int i = 0; i < n; i++)
high += arr[i];
int result = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
if (isSquarePossible(arr, mid)) {
result = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}
return result;
}
int main()
{
vector< int > arr = { 1, 2, 3 };
int largestSquare = largestSquareLength(arr);
cout << largestSquare << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
class GFG {
public static boolean isSquarePossible(List<Integer> arr, int k) {
int count = 0 ;
for ( int i = 0 ; i < arr.size(); i++) {
count += arr.get(i) / k;
if (count >= k)
return true ;
}
return false ;
}
public static int largestSquareLength(List<Integer> arr) {
int n = arr.size();
int low = 1 ;
int high = 0 ;
for ( int i = 0 ; i < n; i++)
high += arr.get(i);
int result = 0 ;
while (low <= high) {
int mid = low + (high - low) / 2 ;
if (isSquarePossible(arr, mid)) {
result = mid;
low = mid + 1 ;
} else {
high = mid - 1 ;
}
}
return result;
}
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>();
arr.add( 1 );
arr.add( 2 );
arr.add( 3 );
int largestSquare = largestSquareLength(arr);
System.out.println(largestSquare);
}
}
|
Python3
def is_square_possible(arr, k):
count = 0
for i in range ( len (arr)):
count + = arr[i] / / k
if count > = k:
return True
return False
def largest_square_length(arr):
n = len (arr)
low = 1
high = 0
for i in range (n):
high + = arr[i]
result = 0
while low < = high:
mid = low + (high - low) / / 2
if is_square_possible(arr, mid):
result = mid
low = mid + 1
else :
high = mid - 1
return result
arr = [ 1 , 2 , 3 ]
largest_square = largest_square_length(arr)
print (largest_square)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static bool IsSquarePossible(List< int > arr, int k)
{
int count = 0;
foreach ( int block in arr)
{
count += block / k;
if (count >= k)
return true ;
}
return false ;
}
public static int LargestSquareLength(List< int > arr)
{
int n = arr.Count;
int low = 1;
int high = 0;
foreach ( int block in arr)
high += block;
int result = 0;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (IsSquarePossible(arr, mid))
{
result = mid;
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return result;
}
public static void Main( string [] args)
{
List< int > arr = new List< int > { 1, 2, 3 };
int largestSquare = LargestSquareLength(arr);
Console.WriteLine(largestSquare);
}
}
|
Javascript
function isSquarePossible(arr, k) {
let count = 0;
for (let block of arr) {
count += Math.floor(block / k);
if (count >= k) {
return true ;
}
}
return false ;
}
function largestSquareLength(arr) {
const n = arr.length;
let low = 1;
let high = 0;
for (let block of arr) {
high += block;
}
let result = 0;
while (low <= high) {
const mid = Math.floor(low + (high - low) / 2);
if (isSquarePossible(arr, mid)) {
result = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
const arr = [1, 2, 3];
const largestSquare = largestSquareLength(arr);
console.log(largestSquare);
|
Time Complexity: O(n log M), where n is the number of elements in the input array and M is the sum of all elements in the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...