![]() |
VOOZH | about |
Given a:b and b:c. The task is to write a program to find ratio a:b:c
Examples:
Input: a:b = 2:3, b:c = 3:4
Output: 2:3:4
Input: a:b = 3:4, b:c = 8:9
Output: 6:8:9
Learn About : Ratio and Proportions
Approach: The trick is to make the common term ‘b’ equal in both ratios. Therefore, multiply the first ratio by b2 (b term of second ratio) and the second ratio by b1.
Given: a:b1 and b2:c
Solution: a:b:c = (a*b2):(b1*b2):(c*b1)
For example:
If a : b = 5 : 9 and b : c = 7 : 4, then find a : b : c.
Solution:
Here, Make the common term ‘b’ equal in both ratios.
Therefore, multiply the first ratio by 7 and the second ratio by 9.
So, a : b = 35 : 63 and b : c = 63 : 36
Thus, a : b : c = 35 : 63 : 36
Below is the implementation of the above approach:
6:8:9
Time Complexity : O(log(A+B)) ,where A=a*b2 and B = b1*b2
Space Complexity : O(1), since no extra space has been taken.