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 square void 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 Code int main() { int N = 6; // Given array arr[] int arr[] = { 3, 2, 1, 5, 2, 4 }; // Function Call maxSide(arr, N); return 0; } |
Java
// Java implementation of the above approach import java.util.Arrays; class GFG{ // Function to find maximum side // length of square static 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 code public 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 |
C#
// C# implementation of the above approach using System; class GFG{ // Function to find maximum side // length of square static 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 code public 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 |
3
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Area of the largest square that can be formed from the given length sticks using Hashing
- Minimum number of blocks required to form Hollow Rectangular Prism
- Height of Pyramid formed with given Rectangular Box
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Maximum of smallest possible area that can get with exactly k cut of given rectangular
- Find the largest number that can be formed with the given digits
- Minimum number of square tiles required to fill the rectangular floor
- Find the largest area rectangular sub-matrix whose sum is equal to k
- Largest factor of a given number which is a perfect square
- Number of ways to color N-K blocks using given operation
- Largest even number that can be formed by any number of swaps
- Find the largest number that can be formed by changing at most K digits
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Largest N digit Octal number which is a Perfect square
- Number of times the largest perfect square number can be subtracted from N
- Largest square that can be inscribed in a semicircle
- Area of the Largest square that can be inscribed in an ellipse
- Largest Square that can be inscribed within a hexagon
- Largest hexagon that can be inscribed within a square
- Largest area square in an array when elements can be shuffled
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


