# Minimum area of square holding two identical rectangles

Given the length** L** and breadth **B** of two identical rectangles, the task is to find the minimum area of a square in which the two identical rectangles with dimensions **L × B** can be embedded.

**Examples:**

Input:L = 7, B = 4

Output:64

Explanation:Two rectangles with sides 7 x 4 can fit into square with side 8. By placing two rectangles with side 4 upon each other and the length of contact is 7.

Input:L = 1, B = 3

Output:9

Explanation:Two rectangles with sides 1 x 3 can fit into square with side 3. By placing two rectangles with side 1 upon each other and a gap of 1 between the 2 rectangles.

**Approach:**

- If one side of the rectangle is lesser than or equal to half the length of the other side then the side of the square is the longer side of the rectangle.
- If twice the length of the smaller side is greater than the larger side, then the side of the square is twice the length of the smaller side of the rectangle.

Below is the implementation of the above approach:

## C++

`// C++ program for the above problem ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the ` `// area of the square ` `int` `areaSquare(` `int` `L, ` `int` `B) ` `{ ` ` ` `// Larger side of rectangle ` ` ` `int` `large = max(L, B); ` ` ` ` ` `// Smaller side of the rectangle ` ` ` `int` `small = min(L, B); ` ` ` ` ` `if` `(large >= 2 * small) ` ` ` `return` `large * large; ` ` ` `else` ` ` `return` `(2 * small) * (2 * small); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `L = 7; ` ` ` `int` `B = 4; ` ` ` `cout << areaSquare(L, B); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.io.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the ` `// area of the square ` `static` `int` `areaSquare(` `int` `L, ` `int` `B) ` `{ ` ` ` ` ` `// Larger side of rectangle ` ` ` `int` `large = Math.max(L, B); ` ` ` ` ` `// Smaller side of the rectangle ` ` ` `int` `small = Math.min(L, B); ` ` ` ` ` `if` `(large >= ` `2` `* small) ` ` ` `{ ` ` ` `return` `large * large; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `return` `(` `2` `* small) * (` `2` `* small); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `L = ` `7` `; ` ` ` `int` `B = ` `4` `; ` ` ` ` ` `System.out.println(areaSquare(L, B)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above problem ` ` ` `# Function to find the ` `# area of the square ` `def` `areaSquare(L, B): ` ` ` ` ` `# Larger side of rectangle ` ` ` `large ` `=` `max` `(L, B) ` ` ` ` ` `# Smaller side of the rectangle ` ` ` `small ` `=` `min` `(L, B) ` ` ` ` ` `if` `(large >` `=` `2` `*` `small): ` ` ` `return` `large ` `*` `large ` ` ` `else` `: ` ` ` `return` `(` `2` `*` `small) ` `*` `(` `2` `*` `small) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `L ` `=` `7` ` ` `B ` `=` `4` ` ` ` ` `print` `(areaSquare(L, B)) ` ` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above problem ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the ` `// area of the square ` `public` `static` `int` `areaSquare(` `int` `L, ` `int` `B) ` `{ ` ` ` ` ` `// Larger side of rectangle ` ` ` `int` `large = Math.Max(L, B); ` ` ` ` ` `// Smaller side of the rectangle ` ` ` `int` `small = Math.Min(L, B); ` ` ` ` ` `if` `(large >= 2 * small) ` ` ` `{ ` ` ` `return` `large * large; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `return` `(2 * small) * (2 * small); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `L = 7; ` ` ` `int` `B = 4; ` ` ` ` ` `Console.Write(areaSquare(L, B)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

64

**Time Complexity:** O(1)

**Auxiilary Space Complexity:** O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Total area of two overlapping rectangles
- Check if N rectangles of equal area can be formed from (4 * N) integers
- Smallest square formed with given rectangles
- Find the minimum number of rectangles left after inserting one into another
- Program for Area Of Square
- Sum of Area of all possible square inside a rectangle
- Area of a square from diagonal length
- Area of square Circumscribed by Circle
- Program to find the area of a Square
- Program for Area Of Square after N-th fold
- Area of a Circumscribed Circle of a Square
- Area of a leaf inside a square
- Area of a largest square fit in a right angle triangle
- Area of the Largest square that can be inscribed in an ellipse
- Largest area square in an array when elements can be shuffled
- Program to find the surface area of the square pyramid
- Area of largest semicircle that can be drawn inside a square
- Program to calculate area of an Circle inscribed in a Square
- Find the area of the shaded region formed by the intersection of four semicircles in a square
- Find number of square of area Z which can be built in a matrix having blocked regions

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.