Given height H, and width W, of a rectangle, the task is to find the side of a square of the minimum area in which two rectangles fit completely.
- Two rectangles can touch each other by side or corner.
- Rectangles cannot intersect each other.
- Rectangles can also touch the sides of the square but must be completely inside it.
- The Rectangles can be rotate also
Input: H = 4, W = 2
Minimum side of the square is 4
Input: H = 4, W = 4
There are three possible cases:
- At first check the given height and width is same or not, if same then rectangles are itself a square, so simply attach two squares and double the side,
- Then if 1st case not satisfied then check if height > width or not,
if greater, then double the width value and add it to the width and make a new width,
new_Width = width + width
- Then find the difference of ‘new_Width’ and ‘height’ and store the difference in a new variable “diff”,
- If the ‘new_Width’ is less than ‘height’, then add the “diff” value to the ‘new_Width’, then the ‘height’ and ‘new_width’ are same and return ‘new_Width’, that is the side of the minimal square.
- If the ‘new_Width’ is greater than ‘height’, then add the “diff” value to the ‘height’, then the ‘height’ and ‘width’ are same, and return ‘height’, that is the side of the minimal square.
- Then if 2nd case not satisfied then check if height < width,
if smaller, then double the ‘height’ value and add it to the height and make a new height,
new_Height = height + height
- Then find the difference of ‘new_Height’ and ‘width’ and store the difference in a new variable “diff”,
- If the ‘new_Height’ is less than ‘width’, then add the “diff” value to the ‘new_Height’, then the ‘width’ and ‘new_Height’ are same and return ‘new_Height’, that is the side of the minimal square.
- If the ‘new_Height’ is greater than ‘width’, then add the “diff” value to the ‘width’, then the ‘height’ and ‘width’ are same, and return ‘height’, that is the side of the minimal square.
Below is the implementation of the above approach:
Time complexity: O(1)
Auxiliary Space: O(1)
- Minimum area of square holding two identical rectangles
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Area of a largest square fit in a right angle triangle
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Area of a Square | Using Side, Diagonal and Perimeter
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Total area of two overlapping rectangles
- Sum of Area of all possible square inside a rectangle
- Area of a leaf inside a square
- Area of largest semicircle that can be drawn inside a square
- Count of smaller rectangles that can be placed inside a bigger rectangle
- Find number of square of area Z which can be built in a matrix having blocked regions
- Check if N rectangles of equal area can be formed from (4 * N) integers
- Count of Rectangles with area K made up of only 1s from given Binary Arrays
- Smallest square formed with given rectangles
- Split N into two integers whose addition to A and B makes them equal
- Find the area of rhombus from given Angle and Side length
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.