Open In App

Minimum area of square holding two identical rectangles

Last Updated : 01 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:
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;
}


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


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


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


Javascript




<script>
 
// Javascript implementation of the above approach
 
// Function to find the
// area of the square
function areaSquare(L, B)
{
       
    // Larger side of rectangle
    let large = Math.max(L, B);
   
    // Smaller side of the rectangle
    let small = Math.min(L, B);
   
    if (large >= 2 * small)
    {
        return large * large;
    }
    else
    {
        return (2 * small) * (2 * small);
    }
}
  
  // Driver Code
     
    let L = 7;
    let B = 4;
       
    document.write(areaSquare(L, B));
      
</script>


Output: 

64

 

Time Complexity: O(1) 
Auxiliary Space Complexity: O(1) 
 



Similar Reads

Find side of Square which makes minimal area to fit two identical rectangles inside it
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
12 min read
Total area of two overlapping rectangles
Given the coordinates of two rectangles in a 2D plane, the first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2) and the second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2). The task is to find the total area covered by the two rectangles. Examples: Input
7 min read
Area of a triangle with two vertices at midpoints of opposite sides of a square and the other vertex lying on vertex of a square
Given a positive integer N representing the side of a square, the task is to find the area of a triangle formed by connecting the midpoints of two adjacent sides and vertex opposite to the two sides. Examples: Input: N = 10Output: 37.5 Input: N = 1Output: 0.375 Approach: The given problem can be solved based on the following observations: The one s
5 min read
Check if N rectangles of equal area can be formed from (4 * N) integers
Given an integer N and an array arr[] of size 4 * N, the task is to check whether N rectangles of equal area can be formed from this array if each element can be used only once. Examples: Input: arr[] = {1, 8, 2, 1, 2, 4, 4, 8}, N = 2 Output: Yes Two rectangles with sides (1, 8, 1, 8) and (2, 4, 2, 4) can be formed. Both of these rectangles have th
6 min read
Count of Rectangles with area K made up of only 1s from given Binary Arrays
Given two binary arrays A[] and B[], of length N and M respectively, the task is to find the number of rectangles of area K consisting of 1's in the matrix C[][] generated by multiplying the two arrays such that, C[i][j] = A[i] * B[j] (1&lt; i &lt; n, 1&lt; j &lt; m). Examples: Input: N= 3, M = 3, A[] = {1, 1, 0}, B[] = {0, 1, 1}, K = 2 Output: 4 E
11 min read
Number of rectangles with given area in an N*M grid
Given three positive integers N, M, and A, the task is to count the number of rectangles with area equal to A present in an M * N grid. Examples: Input: N = 2, M = 2, A = 2 Output: 4 Explanation: In the given grid of size 2 × 2, 2 rectangles of dimension 2 × 1 and 2 rectangles of dimension 1 × 2 can be inscribed. Therefore, the required output is 4
10 min read
Find area of the Circle when the area of inscribed Square is given
Given the area of a square inscribed in a circle as N, the task is to calculate the area of a circle in which the square is inscribed. Examples: Input: N = 4 Output: 6.283 Input: N = 10Output: 15.707 Approach: Consider the below image: Let the area of the square is 'A'The side of the square is given by = A**(1/2)A right-angled triangle is formed by
4 min read
Smallest square formed with given rectangles
Given a rectangle of length l and breadth b, we need to find the area of the smallest square which can be formed with the rectangles of these given dimensions. Examples: Input : 1 2 Output : 4 We can form a 2 x 2 square using two rectangles of size 1 x 2. Input : 7 10 Output :4900 Let's say we want to make a square of side length a from rectangles
6 min read
Size of smallest square that contains N non-overlapping rectangles of given dimensions
Given two positive integers W and H and N rectangles of dimension W*H, the task is to find the smallest size of the square required such that all the N rectangles can be packed without overlapping. Examples: Input: N = 10, W = 2, H = 3Output: 9Explanation:The smallest size of the square is 9 units to pack the given 10 rectangles of size 2*3 as illu
7 min read
Minimum cost to make two strings identical by deleting the digits
Given two strings X and Y consisting of only digits '0' to '9'. Find minimum cost required to make the given two strings identical. Only operation allowed is to delete characters from any of the string. The cost of operation of deleting the digit 'd' is d units. Input: X = 3759, Y = 9350 Output: 23 Explanation For making both string identical, dele
16 min read