VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-maximum-rational-number-rational-numbers-array/

⇱ Maximum rational number (or fraction) from an array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum rational number (or fraction) from an array

Last Updated : 9 Jan, 2023

Given rational numbers, the task is to find the maximum rational number.


Examples: 

Input : ra_num = {{1, 2},
 {2, 3},
 {3, 4},
 {4, 5}};
Output : 4 5

Input : ra_num = {{10, 12},
 {12, 33},
 {33, 14},
 {14, 15}};
Output : 33 14

A simple solution is to find float values and compare the float values. The float computations may cause precision errors. We can avoid them using the below approach.
Say numbers are 1/2, 2/3, 3/4, 4/5
First take an LCM of (2, 3, 4, 5) which is the denominator of all rational numbers. So the LCM of this is 60, then divide with all denominator's and multiple with all numerators, so the value of numerators are (30, 40, 45, 48) 
Then find the max between these rational numbers. So here the last numerator is max then print the last rational number, which is 4/5.

Output: 

4 5

Time Complexity: O(N*log(K)) where N is the size of the given array and K can be the largest denominator of the array.

Auxiliary Space: O(N)
 

Comment