# Maximum rational number (or fraction) from an array

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 below approach.

Say number’s 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 number. so the LCM of this is 60, then divide with all denominator’s and multiple with all numerator’s, so the value of numerator’s are (30, 40, 45, 48)
Then find the max between these rational number’s. so here the last numerator is max then print last rational number. which is 4/5.

## C++

 `// CPP program to find the maximum rational ` `// number in an array. ` `#include ` `using` `namespace` `std; ` ` `  `struct` `Rational { ` ` `  `    ``// numerator and Denominator ` `    ``int` `nume, deno; ` `}; ` ` `  `// here we find the Denominator LCM ` `int` `lcmOfDenominator(vector ra_num) ` `{ ` `    ``// get the first Denominator as lcm ` `    ``int` `lcm = ra_num[0].deno; ` `    ``int` `i; ` ` `  `    ``// find the lcm of all relational  ` `    ``// number Denominator ` `    ``for` `(i = 1; i < ra_num.size(); i++)  ` `        ``lcm = (lcm * (ra_num[i].deno)) / ` `                 ``__gcd(lcm, ra_num[i].deno); ` `     `  ` `  `    ``// return the lcm ` `    ``return` `lcm; ` `} ` ` `  `int` `maxRational(vector ra_num) ` `{ ` `    ``// take a temp array for find ` `    ``// maximum numerator after multiple ` `    ``int` `temp[ra_num.size()] = { 0 }; ` ` `  `    ``// get here the lcm of all rational  ` `    ``//number denominator ` `    ``int` `lcm = lcmOfDenominator(ra_num); ` ` `  `    ``// take maximum for get maximum index ` `    ``int` `maximum = 0; ` `    ``int` `maximumind = 0; ` ` `  `    ``// find the index which contain maximum value ` `    ``for` `(``int` `i = 0; i < ra_num.size(); i++) { ` ` `  `        ``// divide lcm with denominator ` `        ``// and multiple with numerator ` `        ``temp[i] = (ra_num[i].nume) * ` `                  ``(lcm / ra_num[i].deno); ` ` `  `        ``// get the maximum numerator ` `        ``if` `(maximum < temp[i]) { ` `            ``maximum = temp[i]; ` `            ``maximumind = i; ` `        ``} ` `    ``} ` ` `  `    ``// return index which contain ` `    ``// maximum rational number ` `    ``return` `maximumind; ` `} ` ` `  `int` `main() ` `{ ` `    ``// given rational number ` `    ``vector ra_num = { { 1, 2 }, ` `                                ``{ 2, 3 }, ` `                                ``{ 3, 4 }, ` `                                ``{ 4, 5 } }; ` ` `  `    ``// get the index which contain maximum value ` `    ``int` `index_max = maxRational(ra_num); ` ` `  `    ``// print numerator and denominator ` `    ``cout << ra_num[index_max].nume << ``" "`  `         ``<< ra_num[index_max].deno << ``"\n"``; ` `} `

## Java

 `// Java program to find the maximum rational  ` `// number in an array.  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `static` `class` `Rational ` `{  ` ` `  `    ``// numerator and Denominator  ` `    ``int` `nume, deno;  ` ` `  `    ``public` `Rational(``int` `nume, ``int` `deno) ` `    ``{ ` `        ``this``.nume = nume; ` `        ``this``.deno = deno; ` `    ``} ` `         `  `};  ` ` `  `// here we find the Denominator LCM  ` `static` `int` `lcmOfDenominator(Vector ra_num)  ` `{  ` `    ``// get the first Denominator as lcm  ` `    ``int` `lcm = ra_num.get(``0``).deno;  ` `    ``int` `i;  ` ` `  `    ``// find the lcm of all relational  ` `    ``// number Denominator  ` `    ``for` `(i = ``1``; i < ra_num.size(); i++)  ` `        ``lcm = (lcm * (ra_num.get(i).deno)) /  ` `                ``__gcd(lcm, ra_num.get(i).deno);  ` `     `  `    ``// return the lcm  ` `    ``return` `lcm;  ` `}  ` ` `  `static` `int` `maxRational(Vector ra_num)  ` `{  ` `    ``// take a temp array for find  ` `    ``// maximum numerator after multiple  ` `    ``int` `[]temp = ``new` `int``[ra_num.size()]; ` `     `  `    ``// get here the lcm of all rational  ` `    ``//number denominator  ` `    ``int` `lcm = lcmOfDenominator(ra_num);  ` ` `  `    ``// take maximum for get maximum index  ` `    ``int` `maximum = ``0``;  ` `    ``int` `maximumind = ``0``;  ` ` `  `    ``// find the index which contain maximum value  ` `    ``for` `(``int` `i = ``0``; i < ra_num.size(); i++) ` `    ``{  ` ` `  `        ``// divide lcm with denominator  ` `        ``// and multiple with numerator  ` `        ``temp[i] = (ra_num.get(i).nume) *  ` `                  ``(lcm / ra_num.get(i).deno);  ` ` `  `        ``// get the maximum numerator  ` `        ``if` `(maximum < temp[i])  ` `        ``{  ` `            ``maximum = temp[i];  ` `            ``maximumind = i;  ` `        ``}  ` `    ``}  ` ` `  `    ``// return index which contain  ` `    ``// maximum rational number  ` `    ``return` `maximumind;  ` `}  ` ` `  `static` `int` `__gcd(``int` `a, ``int` `b)  ` `{  ` `    ``if` `(b == ``0``)  ` `        ``return` `a;  ` `    ``return` `__gcd(b, a % b);  ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``// given rational number  ` `    ``Vector ra_num = ``new` `Vector(); ` `        ``ra_num.add(``new` `Rational( ``1``, ``2` `));  ` `    ``ra_num.add(``new` `Rational( ``2``, ``3` `));                          ` `    ``ra_num.add(``new` `Rational( ``3``, ``4` `));                          ` `    ``ra_num.add(``new` `Rational( ``4``, ``5` `));                          ` ` `  `    ``// get the index which contain maximum value  ` `    ``int` `index_max = maxRational(ra_num);  ` ` `  `    ``// print numerator and denominator  ` `    ``System.out.println(ra_num.get(index_max).nume +  ` `                 ``" "` `+ ra_num.get(index_max).deno);  ` `    ``} ` `} ` ` `  `// This code is contributed by Princi Singh `

## C#

 `// C# program to find the maximum rational  ` `// number in an array. ` `using` `System; ` `using` `System.Collections.Generic;  ` ` `  `class` `GFG  ` `{ ` ` `  `public` `class` `Rational ` `{  ` ` `  `    ``// numerator and Denominator  ` `    ``public` `int` `nume, deno;  ` ` `  `    ``public` `Rational(``int` `nume, ``int` `deno) ` `    ``{ ` `        ``this``.nume = nume; ` `        ``this``.deno = deno; ` `    ``} ` `         `  `};  ` ` `  `// here we find the Denominator LCM  ` `static` `int` `lcmOfDenominator(List ra_num)  ` `{  ` `    ``// get the first Denominator as lcm  ` `    ``int` `lcm = ra_num[0].deno;  ` `    ``int` `i;  ` ` `  `    ``// find the lcm of all relational  ` `    ``// number Denominator  ` `    ``for` `(i = 1; i < ra_num.Count; i++)  ` `        ``lcm = (lcm * (ra_num[i].deno)) /  ` `                ``__gcd(lcm, ra_num[i].deno);  ` `     `  `    ``// return the lcm  ` `    ``return` `lcm;  ` `}  ` ` `  `static` `int` `maxRational(List ra_num)  ` `{  ` `    ``// take a temp array for find  ` `    ``// maximum numerator after multiple  ` `    ``int` `[]temp = ``new` `int``[ra_num.Count]; ` `     `  `    ``// get here the lcm of all rational  ` `    ``//number denominator  ` `    ``int` `lcm = lcmOfDenominator(ra_num);  ` ` `  `    ``// take maximum for get maximum index  ` `    ``int` `maximum = 0;  ` `    ``int` `maximumind = 0;  ` ` `  `    ``// find the index which contain maximum value  ` `    ``for` `(``int` `i = 0; i < ra_num.Count; i++) ` `    ``{  ` ` `  `        ``// divide lcm with denominator  ` `        ``// and multiple with numerator  ` `        ``temp[i] = (ra_num[i].nume) *  ` `                  ``(lcm / ra_num[i].deno);  ` ` `  `        ``// get the maximum numerator  ` `        ``if` `(maximum < temp[i])  ` `        ``{  ` `            ``maximum = temp[i];  ` `            ``maximumind = i;  ` `        ``}  ` `    ``}  ` ` `  `    ``// return index which contain  ` `    ``// maximum rational number  ` `    ``return` `maximumind;  ` `}  ` ` `  `static` `int` `__gcd(``int` `a, ``int` `b)  ` `{  ` `    ``if` `(b == 0)  ` `        ``return` `a;  ` `    ``return` `__gcd(b, a % b);  ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``// given rational number  ` `    ``List ra_num = ``new` `List(); ` `                   ``ra_num.Add(``new` `Rational( 1, 2 ));  ` `                   ``ra_num.Add(``new` `Rational( 2, 3 ));                          ` `                   ``ra_num.Add(``new` `Rational( 3, 4 ));                          ` `                   ``ra_num.Add(``new` `Rational( 4, 5 ));                          ` ` `  `    ``// get the index which contain maximum value  ` `    ``int` `index_max = maxRational(ra_num);  ` ` `  `    ``// print numerator and denominator  ` `    ``Console.WriteLine(ra_num[index_max].nume +  ` `                ``" "` `+ ra_num[index_max].deno);  ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```4 5
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.