VOOZH about

URL: https://www.geeksforgeeks.org/dsa/hcf-of-array-of-fractions-or-rational-numbers/

⇱ HCF of array of fractions (or rational numbers) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

HCF of array of fractions (or rational numbers)

Last Updated : 2 Aug, 2022

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:


Output
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.
 

Comment
Article Tags: