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)
Auxillary Space: O(1)
- Minimum area of square holding two identical rectangles
- Area of a leaf inside a square
- Sum of Area of all possible square inside a rectangle
- Area of a Square | Using Side, Diagonal and Perimeter
- Area of largest semicircle that can be drawn inside a square
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Find the Side of the smallest Square that can contain given 4 Big Squares
- Program to find the side of the Octagon inscribed within the square
- Find the number of squares inside the given square grid
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Program to find the area of a Square
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Program to find the surface area of the square pyramid
- Find number of square of area Z which can be built in a matrix having blocked regions
- Find the area of the shaded region formed by the intersection of four semicircles in a square
- 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
- Area of a triangle inside a parallelogram
- Program to calculate area of a rhombus whose one side and diagonal are given
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.