![]() |
VOOZH | about |
Given a fraction series. Find the H.C.F of a given fraction series.
Examples:
Input : [{2, 5}, {8, 9}, {16, 81}, {10, 27}]
Output : 2, 405
Explanation : 2/405 is the largest number that
divides all 2/5, 8/9, 16/81 and 10/27.
Input : [{9, 10}, {12, 25}, {18, 35}, {21, 40}]
Output : 3, 1400
Approach:
Find the H.C.F of numerators. Find the L.C.M of denominators. Calculate fraction of H.C.F/L.C.M. Reduce the fraction to Lowest Fraction.
Implementation:
3, 1400
Time Complexity: O(n log(a)) , where a is the maximum element in array
Auxiliary Space: O(log(a)), where a is the maximum element in array
Please suggest if someone has a better solution which is more efficient in terms of space and time.