Problem Statement: Adding two fractional numbers.
Examples:
Example 1: Input: 3/4 + 1/7 Output: 25/28 Explanation: Since 3/4 + 1/7 = 25/28 Example 2: Input: 5/2 +1/2 Output: 3/1 Explanation: Since 5/2 + 1/2 = 3/1
Solution:
Intuition: The denominator will be lcm of den1 and den2.Then change the num1 and num2 as we changed den1 and den2 to lcm and then add the new num1 and num2 to get the new numerator.
Approach:
- We will maintain num3 and den3 to store answers.
- First find gcd of den1 and den2 using euclidean’s algorithm.Once we get the gcd we can easily find lcm.lcm(den1,den2) is (den1*den2)/gcd(den1,den2).
- Now we will have to change num1 and num2 to num1*(den3/den1) and num2*(den3/den2),since we changed den1 and den2 to lcm.
- Now add num1 and num2 and store it in the num3.
- Now we have num3 and den3.The answer should be num3/den3,but before printing the answer we should convert it into the simplest form.
- To convert it into the simplest form,divide numerator and denominator both by gcd of numerator and denominator.
- Print num3 / den3.
Code:
C++ Code
#include<bits/stdc++.h>
using namespace std;
int _gcd(int a, int b) {
if (b == 0) {
return a;
}
return _gcd(b, a % b);
}
void simple(int &num3, int &den3) {
int gcd = _gcd(num3, den3);
//dividing num and den by gcd to get the fraction
//in the simplest form.
num3 /= gcd;
den3 /= gcd;
}
int main()
{
//num3 and den3 stores the numerator and denominator of the final answer
int num1 = 3, den1 = 4, num2 = 1, den2 = 7, num3, den3;
int lcm = (den1 * den2) / _gcd(den1, den2);
//answer's denominator will be lcm of den1 and den2
den3 = lcm;
//changing numerators to have same denominator and then adding
num3 = num1 * (den3 / den1) + num2 * (den3 / den2);
simple(num3, den3);
cout << num1 << "/" << den1 << " + " << num2 << "/" << den2 << " = " << num3 << "/"
<< den3;
}
Output: 3/4 + 1/7 = 25/28
Time Complexity: O(log(min(den1,den2))),since we are using euclidean’s algorithm.
Space Complexity: O(1).
Java Code
public class Main {
static int _gcd(int a, int b) {
if (b == 0) {
return a;
}
return _gcd(b, a % b);
}
static void simple(int num3, int den3) {
int gcd = _gcd(num3, den3);
//dividing num and den by gcd to get the fraction
//in the simplest form.
num3 /= gcd;
den3 /= gcd;
}
public static void main(String args[]) {
int num1 = 3, den1 = 4, num2 = 1, den2 = 7, num3, den3;
int lcm = (den1 * den2) / _gcd(den1, den2);
//answer's denominator will be lcm of den1 and den2
den3 = lcm;
//changing numerators to have same denominator and then adding
num3 = num1 * (den3 / den1) + num2 * (den3 / den2);
simple(num3, den3);
System.out.println(num1 + "/" + den1 + " + " + num2 + "/" + den2 + " = " + num3 + "/" +
den3);
}
}
Output: 3/4 + 1/7 = 25/28
Time Complexity: O(log(min(den1,den2))), since we are using euclidean’s algorithm.
Space Complexity: O(1).
Special thanks to Pranav Padawe for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article