Largest square which can be formed using given rectangular blocks
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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to find maximum side// length of squarevoid maxSide(int a[], int n){ int sideLength = 0; // Sort array in asc order sort(a, a + n); // Traverse array in desc order for (int i = n - 1; i >= 0; i--) { if (a[i] > sideLength) { sideLength++; } else { break; } } cout << sideLength << endl;}// Driver Codeint main(){ int N = 6; // Given array arr[] int arr[] = { 3, 2, 1, 5, 2, 4 }; // Function Call maxSide(arr, N); return 0;} |
Java
// Java program for the above approachimport java.util.Arrays;class GFG{ // Function to find maximum side// length of squarestatic void maxSide(int a[], int n){ int sideLength = 0; // Sort array in asc order Arrays.sort(a); // Traverse array in desc order for(int i = n - 1; i >= 0; i--) { if (a[i] > sideLength) { sideLength++; } else { break; } } System.out.println(sideLength);} // Driver codepublic static void main (String[] args){ int N = 6; // Given array arr[] int arr[] = new int[]{ 3, 2, 1, 5, 2, 4 }; // Function Call maxSide(arr, N);}}// This code is contributed by Pratima Pandey |
Python3
# Python3 program for the above approach# Function to find maximum side# length of squaredef maxSide(a, n): sideLength = 0 # Sort array in asc order a.sort # Traverse array in desc order for i in range(n - 1, -1, -1): if (a[i] > sideLength): sideLength += 1 else: break print(sideLength) # Driver codeN = 6# Given array arr[]arr = [ 3, 2, 1, 5, 2, 4 ]# Function CallmaxSide(arr, N)# This code is contributed by divyeshrabadiya07 |
C#
// C# program for the above approachusing System;class GFG{ // Function to find maximum side// length of squarestatic void maxSide(int []a, int n){ int sideLength = 0; // Sort array in asc order Array.Sort(a); // Traverse array in desc order for(int i = n - 1; i >= 0; i--) { if (a[i] > sideLength) { sideLength++; } else { break; } } Console.Write(sideLength);} // Driver codepublic static void Main(){ int N = 6; // Given array arr[] int []arr = new int[]{ 3, 2, 1, 5, 2, 4 }; // Function Call maxSide(arr, N);}}// This code is contributed by Code_Mech |
Javascript
<script>// Javascript program for the above approach // Function to find maximum side // length of square function maxSide( a, n) { let sideLength = 0; // Sort array in asc order a.sort(); // Traverse array in desc order for ( i = n - 1; i >= 0; i--) { if (a[i] > sideLength) { sideLength++; } else { break; } } document.write(sideLength); } // Driver code let N = 6; // Given array arr let arr = [3, 2, 1, 5, 2, 4 ]; // Function Call maxSide(arr, N); // This code contributed by aashish1995</script> |
Output:
3
Time Complexity: O(N*log N)
Auxiliary Space: O(1)






Please Login to comment...