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.
Note:
- 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
Example :
Input: H = 4, W = 2
Output: 4
Explanation:
Minimum side of the square is 4
Input: H = 4, W = 4
Output: 8
Approach :
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.
- End

Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimalSquareSide( int a, int b)
{
if (a == b) {
return 2 * a;
}
if (a != b) {
if (a > b) {
int newB = b + b;
int diff = abs (newB - a);
if (newB < a) {
b = newB + diff;
if (a == b)
return a;
return 0;
}
else {
a = a + diff;
if (a == newB)
return a;
return 0;
}
}
else {
int newA = a + a;
int diff = abs (newA - b);
if (newA < b) {
a = diff + newA;
if (a == b)
return a;
return 0;
}
else {
b = b + diff;
if (b == newA)
return b;
return 0;
}
}
}
}
int main()
{
int H, W;
H = 3, W = 1;
cout << minimalSquareSide(H, W) << endl;
return 0;
}
|
Java
class GFG{
public static int minimalSquareSide( int a, int b)
{
if (a == b)
{
return 2 * a;
}
if (a != b)
{
if (a > b)
{
int newB = b + b;
int diff = Math.abs(newB - a);
if (newB < a)
{
b = newB + diff;
if (a == b)
return a;
return 0 ;
}
else
{
a = a + diff;
if (a == newB)
return a;
return 0 ;
}
}
else
{
int newA = a + a;
int diff = Math.abs(newA - b);
if (newA < b)
{
a = diff + newA;
if (a == b)
return a;
return 0 ;
}
else
{
b = b + diff;
if (b == newA)
return b;
return 0 ;
}
}
}
return 0 ;
}
public static void main(String[] args)
{
int H, W;
H = 3 ; W = 1 ;
System.out.println(minimalSquareSide(H, W));
}
}
|
Python3
def minimalSquareSide(a, b):
if (a = = b):
return 2 * a
if (a ! = b):
if (a > b):
newB = b + b
diff = abs (newB - a)
if (newB < a):
b = newB + diff
if (a = = b):
return a
return 0
else :
a = a + diff
if (a = = newB):
return a
return 0
else :
newA = a + a
diff = abs (newA - b)
if (newA < b):
a = diff + newA
if (a = = b):
return a
return 0
else :
b = b + diff
if (b = = newA):
return b
return 0
H = 3
W = 1
print (minimalSquareSide(H, W))
|
C#
using System;
class GFG{
public static int minimalSquareSide( int a, int b)
{
if (a == b)
{
return 2 * a;
}
if (a != b)
{
if (a > b)
{
int newB = b + b;
int diff = Math.Abs(newB - a);
if (newB < a)
{
b = newB + diff;
if (a == b)
return a;
return 0;
}
else
{
a = a + diff;
if (a == newB)
return a;
return 0;
}
}
else
{
int newA = a + a;
int diff = Math.Abs(newA - b);
if (newA < b)
{
a = diff + newA;
if (a == b)
return a;
return 0;
}
else
{
b = b + diff;
if (b == newA)
return b;
return 0;
}
}
}
return 0;
}
public static void Main(String[] args)
{
int H, W;
H = 3; W = 1;
Console.WriteLine(minimalSquareSide(H, W));
}
}
|
Javascript
<script>
function minimalSquareSide(a , b)
{
if (a == b)
{
return 2 * a;
}
if (a != b)
{
if (a > b)
{
var newB = b + b;
var diff = Math.abs(newB - a);
if (newB < a)
{
b = newB + diff;
if (a == b)
return a;
return 0;
}
else
{
a = a + diff;
if (a == newB)
return a;
return 0;
}
}
else
{
var newA = a + a;
var diff = Math.abs(newA - b);
if (newA < b)
{
a = diff + newA;
if (a == b)
return a;
return 0;
}
else
{
b = b + diff;
if (b == newA)
return b;
return 0;
}
}
}
return 0;
}
var H, W;
H = 3; W = 1;
document.write(minimalSquareSide(H, W));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)