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 = 2
Output: 2:3
The dimensions can be 3X3 and 3X2.
Input: a = 4, b = 3, c = 2, d = 2
Output: 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++
#include <bits/stdc++.h>
using namespace std;
void printRatio( int a, int b, int c, int d)
{
if (b * c > a * d) {
swap(c, d);
swap(a, b);
}
int lcm = (a * c) / __gcd(a, c);
int x = lcm / a;
b *= x;
int y = lcm / c;
d *= y;
int k = __gcd(b, d);
b /= k;
d /= k;
cout << b << ":" << d;
}
int main()
{
int a = 4, b = 3, c = 2, d = 2;
printRatio(a, b, c, d);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
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;
}
int lcm = (a * c) / __gcd(a, c);
int x = lcm / a;
b *= x;
int y = lcm / c;
d *= y;
int k = __gcd(b, d);
b /= k;
d /= k;
System.out.print( b + ":" + d);
}
public static void main (String[] args) {
int a = 4 , b = 3 , c = 2 , d = 2 ;
printRatio(a, b, c, d);
}
}
|
Python3
import math
def printRatio(a, b, c, d):
if (b * c > a * d):
swap(c, d)
swap(a, b)
lcm = (a * c) / math.gcd(a, c)
x = lcm / a
b = int (b * x)
y = lcm / c
d = int (d * y)
k = math.gcd(b,d)
b = int (b / k)
d = int (d / k)
print (b, ":" ,d)
if __name__ = = '__main__' :
a = 4
b = 3
c = 2
d = 2
printRatio(a, b, c, d)
|
C#
using System;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
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;
}
int lcm = (a * c) / __gcd(a, c);
int x = lcm / a;
b *= x;
int y = lcm / c;
d *= y;
int k = __gcd(b, d);
b /= k;
d /= k;
Console.WriteLine( b + ":" + d);
}
public static void Main () {
int a = 4, b = 3, c = 2, d = 2;
printRatio(a, b, c, d);
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0)
return $b ;
if ( $b == 0)
return $a ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function printRatio( $a , $b , $c , $d )
{
if ( $b * $c > $a * $d )
{
$temp = $c ;
$c = $d ;
$d = $c ;
$temp = $a ;
$a = $b ;
$b = $temp ;
}
$lcm = ( $a * $c ) / __gcd( $a , $c );
$x = $lcm / $a ;
$b *= $x ;
$y = $lcm / $c ;
$d *= $y ;
$k = __gcd( $b , $d );
$b /= $k ;
$d /= $k ;
echo $b . ":" . $d ;
}
$a = 4; $b = 3; $c = 2; $d = 2;
printRatio( $a , $b , $c , $d );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b,b);
return __gcd(a, b - a);
}
function printRatio(a, b, c, d)
{
if (b * c > a * d)
{
temp = c;
c = d;
d = c;
temp = a;
a = b;
b = temp;
}
let lcm = (a * c) / __gcd(a, c);
let x = lcm / a;
b *= x;
let y = lcm / c;
d *= y;
let k = __gcd(b, d);
b /= k;
d /= k;
document.write(b + ":" + d);
}
let a = 4, b = 3, c = 2, d = 2;
printRatio(a, b, c, d);
</script>
|
Time complexity: O(log(max(a,c))+log(max(b,d)))
Auxiliary Space: O(log(max(a,c))+log(max(b,d)))