Given two rectangles, X with a ratio of length to width **a:b** and Y with a ratio of length to width **c:d** respectively. Both the rectangles can be resized as long as the ratio of sides remains the same. The task is to place the second rectangle inside the first rectangle such that at least 1 side is equal and that side overlaps of both the rectangles and find the ratio of (space occupied by a 2nd rectangle) : (space occupied by the first rectangle).

**Examples:**

Input:a = 1, b = 1, c = 3, d = 2Output:2:3 The dimensions can be 3X3 and 3X2.Input:a = 4, b = 3, c = 2, d = 2Output:3:4 The dimensions can be 4X3 and 3X3

**Approach:** If we make one of the sides of rectangles equal then the required ratio would be the ratio of the other side.

Consider 2 cases:

- a*d < b*c : We should make a and c equal.
- b*c < a*d : We should make b and d equal.

Since multiplying both sides of a ratio does not change its value. First try to make a and c equal, it can be made equal to their lcm by multiplying (a:b) with lcm/a and (c:d) with lcm/c. After multiplication, the ratio of (b:d) will be the required answer. This ratio can be reduced by dividing b and d with gcd(b, d).

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the ratio ` `void` `printRatio(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` `if` `(b * c > a * d) { ` ` ` `swap(c, d); ` ` ` `swap(a, b); ` ` ` `} ` ` ` ` ` `// LCM of numerators ` ` ` `int` `lcm = (a * c) / __gcd(a, c); ` ` ` ` ` `int` `x = lcm / a; ` ` ` `b *= x; ` ` ` ` ` `int` `y = lcm / c; ` ` ` `d *= y; ` ` ` ` ` `// Answer in reduced form ` ` ` `int` `k = __gcd(b, d); ` ` ` `b /= k; ` ` ` `d /= k; ` ` ` ` ` `cout << b << ` `":"` `<< d; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 4, b = 3, c = 2, d = 2; ` ` ` ` ` `printRatio(a, b, c, d); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` `// Recursive function to return gcd of a and b ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `// Everything divides 0 ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` ` ` `// 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 ratio ` ` ` `static` `void` `printRatio(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` `if` `(b * c > a * d) { ` ` ` `int` `temp = c; ` ` ` `c =d; ` ` ` `d =c; ` ` ` `temp =a; ` ` ` `a =b; ` ` ` `b=temp; ` ` ` ` ` `} ` ` ` ` ` `// LCM of numerators ` ` ` `int` `lcm = (a * c) / __gcd(a, c); ` ` ` ` ` `int` `x = lcm / a; ` ` ` `b *= x; ` ` ` ` ` `int` `y = lcm / c; ` ` ` `d *= y; ` ` ` ` ` `// Answer in reduced form ` ` ` `int` `k = __gcd(b, d); ` ` ` `b /= k; ` ` ` `d /= k; ` ` ` ` ` `System.out.print( b + ` `":"` `+ d); ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `a = ` `4` `, b = ` `3` `, c = ` `2` `, d = ` `2` `; ` ` ` ` ` `printRatio(a, b, c, d); ` ` ` `} ` `} ` ` ` `// This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## Python3

`import` `math ` `# Python3 implementation of above approach ` ` ` `# Function to find the ratio ` `def` `printRatio(a, b, c, d): ` ` ` `if` `(b ` `*` `c > a ` `*` `d): ` ` ` `swap(c, d) ` ` ` `swap(a, b) ` ` ` ` ` `# LCM of numerators ` ` ` `lcm ` `=` `(a ` `*` `c) ` `/` `math.gcd(a, c) ` ` ` ` ` `x ` `=` `lcm ` `/` `a ` ` ` `b ` `=` `int` `(b ` `*` `x) ` ` ` ` ` `y ` `=` `lcm ` `/` `c ` ` ` `d ` `=` `int` `(d ` `*` `y) ` ` ` ` ` `# Answer in reduced form ` ` ` `k ` `=` `math.gcd(b,d) ` ` ` `b ` `=` `int` `(b ` `/` `k) ` ` ` `d ` `=` `int` `(d ` `/` `k) ` ` ` ` ` `print` `(b,` `":"` `,d) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `4` ` ` `b ` `=` `3` ` ` `c ` `=` `2` ` ` `d ` `=` `2` ` ` ` ` `printRatio(a, b, c, d) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` ` ` `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) ` ` ` `return` `b; ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` ` ` `// 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 ratio ` `static` `void` `printRatio(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` `if` `(b * c > a * d) { ` ` ` `int` `temp = c; ` ` ` `c =d; ` ` ` `d =c; ` ` ` `temp =a; ` ` ` `a =b; ` ` ` `b=temp; ` ` ` ` ` `} ` ` ` ` ` `// LCM of numerators ` ` ` `int` `lcm = (a * c) / __gcd(a, c); ` ` ` ` ` `int` `x = lcm / a; ` ` ` `b *= x; ` ` ` ` ` `int` `y = lcm / c; ` ` ` `d *= y; ` ` ` ` ` `// Answer in reduced form ` ` ` `int` `k = __gcd(b, d); ` ` ` `b /= k; ` ` ` `d /= k; ` ` ` ` ` `Console.WriteLine( b + ` `":"` `+ d); ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` `int` `a = 4, b = 3, c = 2, d = 2; ` ` ` ` ` `printRatio(a, b, c, d); ` ` ` `} ` `} ` ` ` `// This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Recursive function to return ` `// gcd of a and b ` `function` `__gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `// Everything divides 0 ` ` ` `if` `(` `$a` `== 0) ` ` ` `return` `$b` `; ` ` ` `if` `(` `$b` `== 0) ` ` ` `return` `$a` `; ` ` ` ` ` `// 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 ratio ` `function` `printRatio(` `$a` `, ` `$b` `, ` `$c` `, ` `$d` `) ` `{ ` ` ` `if` `(` `$b` `* ` `$c` `> ` `$a` `* ` `$d` `) ` ` ` `{ ` ` ` `$temp` `= ` `$c` `; ` ` ` `$c` `= ` `$d` `; ` ` ` `$d` `= ` `$c` `; ` ` ` ` ` `$temp` `= ` `$a` `; ` ` ` `$a` `= ` `$b` `; ` ` ` `$b` `= ` `$temp` `; ` ` ` `} ` ` ` ` ` `// LCM of numerators ` ` ` `$lcm` `= (` `$a` `* ` `$c` `) / __gcd(` `$a` `, ` `$c` `); ` ` ` ` ` `$x` `= ` `$lcm` `/ ` `$a` `; ` ` ` `$b` `*= ` `$x` `; ` ` ` ` ` `$y` `= ` `$lcm` `/ ` `$c` `; ` ` ` `$d` `*= ` `$y` `; ` ` ` ` ` `// Answer in reduced form ` ` ` `$k` `= __gcd(` `$b` `, ` `$d` `); ` ` ` `$b` `/= ` `$k` `; ` ` ` `$d` `/= ` `$k` `; ` ` ` ` ` `echo` `$b` `. ` `":"` `. ` `$d` `; ` `} ` ` ` `// Driver code ` `$a` `= 4; ` `$b` `= 3; ` `$c` `= 2; ` `$d` `= 2; ` ` ` `printRatio(` `$a` `, ` `$b` `, ` `$c` `, ` `$d` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3:4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Area of a square inscribed in a circle which is inscribed in an equilateral triangle
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Area of the biggest possible rhombus that can be inscribed in a rectangle
- Area of Largest rectangle that can be inscribed in an Ellipse
- Area of largest triangle that can be inscribed within a rectangle
- Area of the biggest ellipse inscribed within a rectangle
- Maximum area of rectangle inscribed in an equilateral triangle
- Ratio of mth and nth terms of an A. P. with given ratio of sums
- Find the number which when added to the given ratio a : b, the ratio changes to c : d
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube

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.