Minimum area of square holding two identical rectangles
Last Updated :
01 Sep, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int areaSquare( int L, int B)
{
int large = max(L, B);
int small = min(L, B);
if (large >= 2 * small)
return large * large;
else
return (2 * small) * (2 * small);
}
int main()
{
int L = 7;
int B = 4;
cout << areaSquare(L, B);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int areaSquare( int L, int B)
{
int large = Math.max(L, B);
int small = Math.min(L, B);
if (large >= 2 * small)
{
return large * large;
}
else
{
return ( 2 * small) * ( 2 * small);
}
}
public static void main(String[] args)
{
int L = 7 ;
int B = 4 ;
System.out.println(areaSquare(L, B));
}
}
|
Python3
def areaSquare(L, B):
large = max (L, B)
small = min (L, B)
if (large > = 2 * small):
return large * large
else :
return ( 2 * small) * ( 2 * small)
if __name__ = = '__main__' :
L = 7
B = 4
print (areaSquare(L, B))
|
C#
using System;
class GFG{
public static int areaSquare( int L, int B)
{
int large = Math.Max(L, B);
int small = Math.Min(L, B);
if (large >= 2 * small)
{
return large * large;
}
else
{
return (2 * small) * (2 * small);
}
}
public static void Main()
{
int L = 7;
int B = 4;
Console.Write(areaSquare(L, B));
}
}
|
Javascript
<script>
function areaSquare(L, B)
{
let large = Math.max(L, B);
let small = Math.min(L, B);
if (large >= 2 * small)
{
return large * large;
}
else
{
return (2 * small) * (2 * small);
}
}
let L = 7;
let B = 4;
document.write(areaSquare(L, B));
</script>
|
Time Complexity: O(1)
Auxiliary Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...