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 of length l & b. This means that a is a multiple of both l & b. Since we want the smallest square, it has to be the lowest common multiple (LCM) of l & b.
Program 1:
C++
// C++ Program to find the area // of the smallest square which // can be formed with rectangles // of given dimensions #include <bits/stdc++.h> using namespace std; // Recursive function to return gcd of a and b int gcd(int a, int b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // Base case if (a == b) return a; // a is greater if (a > b) return gcd(a - b, b); return gcd(a, b - a); } // Function to find the area // of the smallest square int squarearea(int l, int b) { // the length or breadth or side // cannot be negative if (l < 0 || b < 0) return -1; // LCM of length and breadth int n = (l * b) / gcd(l, b); // squaring to get the area return n * n; } // Driver code int main() { int l = 6, b = 4; cout << squarearea(l, b) << endl; return 0; } |
Java
// JavaProgram to find the area // of the smallest square which // can be formed with rectangles // of given dimensions class GFG { // Recursive function to // return gcd of a and b static int gcd(int a, int b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // Base case if (a == b) return a; // a is greater if (a > b) return gcd(a - b, b); return gcd(a, b - a); } // Function to find the area // of the smallest square static int squarearea(int l, int b) { // the length or breadth or side // cannot be negative if (l < 0 || b < 0) return -1; // LCM of length and breadth int n = (l * b) / gcd(l, b); // squaring to get the area return n * n; } // Driver code public static void main(String[] args) { int l = 6, b = 4; System.out.println(squarearea(l, b)); } } // This code is contributed // by ChitraNayal |
Python 3
# Python3 Program to find the area # of the smallest square which # can be formed with rectangles # of given dimensions # Recursive function to return gcd of a and b def gcd( a, b): # Everything divides 0 if (a == 0 or b == 0): return 0 # Base case if (a == b): return a # a is greater if (a > b): return gcd(a - b, b) return gcd(a, b - a) # Function to find the area # of the smallest square def squarearea( l, b): # the length or breadth or side # cannot be negative if (l < 0 or b < 0): return -1 # LCM of length and breadth n = (l * b) / gcd(l, b) # squaring to get the area return n * n # Driver code if __name__=='__main__': l = 6 b = 4 print(int(squarearea(l, b))) #This code is contributed by ash264 |
C#
// C# Program to find the area // of the smallest square which // can be formed with rectangles // of given dimensions using System; class GFG { // Recursive function to // return gcd of a and b static int gcd(int a, int b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // Base case if (a == b) return a; // a is greater if (a > b) return gcd(a - b, b); return gcd(a, b - a); } // Function to find the area // of the smallest square static int squarearea(int l, int b) { // the length or breadth or side // cannot be negative if (l < 0 || b < 0) return -1; // LCM of length and breadth int n = (l * b) / gcd(l, b); // squaring to get the area return n * n; } // Driver code public static void Main() { int l = 6, b = 4; Console.Write(squarearea(l, b)); } } // This code is contributed // by ChitraNayal |
PHP
<?php // PHP Program to find the area // of the smallest square which // can be formed with rectangles // of given dimensions // Recursive function to // return gcd of a and b function gcd($a, $b) { // Everything divides 0 if ($a == 0 || $b == 0) return 0; // Base case if ($a == $b) return $a; // a is greater if ($a > $b) return gcd($a - $b, $b); return gcd($a, $b - $a); } // Function to find the area // of the smallest square function squarearea($l, $b) { // the length or breadth or side // cannot be negative if ($l < 0 || $b < 0) return -1; // LCM of length and breadth $n = ($l * $b) / gcd($l, $b); // squaring to get the area return $n * $n; } // Driver code $l = 6; $b = 4; echo squarearea($l, $b)."\n"; // This code is contributed // by ChitraNayal ?> |
144
Recommended Posts:
- Number of unique rectangles formed using N unit squares
- Smallest multiple of N formed using the given set of digits
- Length of the smallest number which is divisible by K and formed by using 1's only
- Find the area of the shaded region formed by the intersection of four semicircles in a square
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Find the Side of the smallest Square that can contain given 4 Big Squares
- Smallest perfect square divisible by all elements of an array
- Check if a number is perfect square without finding square root
- Find if two rectangles overlap
- Sum of Areas of Rectangles possible for an array
- Number of rectangles in N*M grid
- Find all rectangles filled with 0
- Total area of two overlapping rectangles
- Count Distinct Rectangles in N*N Chessboard
- Number of rectangles in a circle of radius R
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



