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 bint 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 squareint 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 codeint 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 dimensionsclass GFG {// Recursive function to // return gcd of a and bstatic int gcd(int a, int b){// Everything divides 0if (a == 0 || b == 0) return 0;// Base caseif (a == b) return a;// a is greaterif (a > b) return gcd(a - b, b);return gcd(a, b - a);}// Function to find the area// of the smallest squarestatic int squarearea(int l, int b){// the length or breadth or side// cannot be negativeif (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 codepublic 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 bdef 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 squaredef 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 codeif __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 dimensionsusing System;class GFG{// Recursive function to // return gcd of a and bstatic int gcd(int a, int b){// Everything divides 0if (a == 0 || b == 0) return 0;// Base caseif (a == b) return a;// a is greaterif (a > b) return gcd(a - b, b);return gcd(a, b - a);}// Function to find the area// of the smallest squarestatic int squarearea(int l, int b){// the length or breadth or side// cannot be negativeif (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 codepublic 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 bfunction 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 squarefunction 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?> |
Javascript
<script>// Javascript 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 bfunction 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 squarefunction squarearea(l , b){// the length or breadth or side// cannot be negativeif (l < 0 || b < 0) return -1; // LCM of length and breadth var n = (l * b) / gcd(l, b); // squaring to get the area return n * n; }// Driver code var l = 6, b = 4; document.write(squarearea(l, b));// This code is contributed by Amit Katiyar </script> |
Output:
144
Time Complexity: O(log(min(l,b)))
Auxiliary Space: O(log(min(l, b)))
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



