


Ayush read about irreducible fractions in the school and made an expression ‘EXPRESSION’ of fraction addition and subtraction.
Note:An irreducible fraction is a fraction in which the numerator and denominator are integers that have no other common divisors than 1.
Example: 2/3, 8/9, 0/1 are irreducible fractions, where 8/12, 9/12 are not irreducible fractions.
Expression ‘EXPRESSION’ contains integers from '0' to '9', and characters '/', '+' and '-'.
Each fraction will be an irreducible fraction and will be in the format of ±(numerator/denominator).
Ayush does not know how to solve the expression he made, so he gave you the expression to solve. Your task is to solve the expression.
Note:Ayush expects your answer to be an irreducible fraction, so if your answer is an integer, convert it into an irreducible fraction by putting 1 in the denominator.
For example, if your answer is "9", then print "9/1" (without quotes).
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains a string ‘EXPRESSION’, representing the expression that you need to solve.
Note:
The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10].
Output format:
For each test case, return the irreducible fraction after solving the expression.
Output for each test case is printed on a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= |EXPRESSION| <= 10^5
‘EXPRESSION’ will only contain integers from '0' to '9', and characters '/', '+' and '-'.
Time Limit: 1 sec
2
1/2+3/4
7/3-5/2
5/4
-1/6
For the first test case,
The first fractional part is 1/2, and the second fractional part is 3/4.
Lcm of denominators of both the fractional part is 4. Hence, solving the expression would give:
(1*2)/(2*2) + 3/4 = (2+3)/4 = 5/4, so you need to return 5/4.
For the second test case,
The first fractional part is 7/3. The second fractional part is 5/2.
Lcm of denominators of both the fractional part is 6. Hence, solving the expression would give:
(7*2)/(3*2) + (5*3)/(2*3) = (14-15)/6 = -1/6, so you need to return -1/6
2
6/5+3/5+1/5
5/3+2/5-1/2
2/1
47/30
Can you solve this problem by making all denominators equal?
We can use brute force to solve the problem. We can simplify this problem by making all denominators equal. This can be done by finding the least common multiple of all the denominators.
First, store the fractions in an array. Then, rewrite the expression such that each fraction will have a common denominator. Now, expression can be easily solved by only solving all the numerators. After that, convert the final expression into an irreducible fraction.
Algorithm:
O(N * log(X)), where ‘N’ is the length of the expression and, ‘X’ is the maximum possible value of the denominator.
Since we are iterating over an expression S of length N. In every iteration, we extract a fraction part and find the least common multiple (LCM) of its denominator with the previously occurred denominators. using the Euclidean GCD algorithm. Euclidean GCD algorithm takes O(log(X)) So overall time complexity will be O(N * log(X)).
O(N), where ‘N’ is the length of the expression.
Since we are using an array of size ‘N’ to store the fractions the space complexity will be O(N).