A recipe-scaling app tracks ingredient amounts as fractions (e.g. 1/2 cup, 3/4 teaspoon). Given a list of fractions, compute their sum and report it in lowest terms.
Each fraction is given as a numerator and a positive denominator (the numerator may be negative, representing a deduction). Output the reduced sum as p/q where q > 0 and gcd(|p|, q) = 1. If the reduced denominator is 1, output just the integer p (no slash). If the sum is exactly zero, output 0.
Line 1: an integer n.
Next n lines: two space-separated integers a and b — the fraction a/b.
The sum of all n fractions, reduced to lowest terms, per the rules above.
Example 1
Input
2 1 2 1 3
Expected
5/6
Explanation
1/2 + 1/3 = 5/6, already in lowest terms.
Example 2
Input
2 1 2 -1 2
Expected
0
Explanation
1/2 + (-1/2) = 0, so the output is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →