Maximum rational number (or fraction) from an array
Last Updated :
09 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.
C++
#include <bits/stdc++.h>
using namespace std;
struct Rational {
int nume, deno;
};
int lcmOfDenominator(vector<Rational> ra_num)
{
int lcm = ra_num[0].deno;
int i;
for (i = 1; i < ra_num.size(); i++)
lcm = (lcm * (ra_num[i].deno)) /
__gcd(lcm, ra_num[i].deno);
return lcm;
}
int maxRational(vector<Rational> ra_num)
{
int temp[ra_num.size()] = { 0 };
int lcm = lcmOfDenominator(ra_num);
int maximum = 0;
int maximumind = 0;
for ( int i = 0; i < ra_num.size(); i++) {
temp[i] = (ra_num[i].nume) *
(lcm / ra_num[i].deno);
if (maximum < temp[i]) {
maximum = temp[i];
maximumind = i;
}
}
return maximumind;
}
int main()
{
vector<Rational> ra_num = { { 1, 2 },
{ 2, 3 },
{ 3, 4 },
{ 4, 5 } };
int index_max = maxRational(ra_num);
cout << ra_num[index_max].nume << " "
<< ra_num[index_max].deno << "\n" ;
}
|
Java
import java.util.*;
class GFG
{
static class Rational
{
int nume, deno;
public Rational( int nume, int deno)
{
this .nume = nume;
this .deno = deno;
}
};
static int lcmOfDenominator(Vector<Rational> ra_num)
{
int lcm = ra_num.get( 0 ).deno;
int i;
for (i = 1 ; i < ra_num.size(); i++)
lcm = (lcm * (ra_num.get(i).deno)) /
__gcd(lcm, ra_num.get(i).deno);
return lcm;
}
static int maxRational(Vector<Rational> ra_num)
{
int []temp = new int [ra_num.size()];
int lcm = lcmOfDenominator(ra_num);
int maximum = 0 ;
int maximumind = 0 ;
for ( int i = 0 ; i < ra_num.size(); i++)
{
temp[i] = (ra_num.get(i).nume) *
(lcm / ra_num.get(i).deno);
if (maximum < temp[i])
{
maximum = temp[i];
maximumind = i;
}
}
return maximumind;
}
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
public static void main(String[] args)
{
Vector<Rational> ra_num = new Vector<Rational>();
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 ));
int index_max = maxRational(ra_num);
System.out.println(ra_num.get(index_max).nume +
" " + ra_num.get(index_max).deno);
}
}
|
Python3
class Rational:
def __init__( self , nume, deno):
self .nume = nume
self .deno = deno
def computeGCD(x, y):
while (y):
x, y = y, x % y
return x
def lcmOfDenominator(ra_num):
lcm = ra_num[ 0 ].deno
for i in range ( 1 , len (ra_num)):
lcm = ((lcm * (ra_num[i].deno)) / /
computeGCD(lcm, ra_num[i].deno))
return lcm
def maxRational(ra_num):
temp = [ 0 for i in range ( len (ra_num))]
lcm = lcmOfDenominator(ra_num)
maximum = 0
maximumind = 0
for i in range ( len (ra_num)):
temp[i] = ((ra_num[i].nume) *
(lcm / / ra_num[i].deno))
if (maximum < temp[i]):
maximum = temp[i]
maximumind = i
return maximumind
if __name__ = = "__main__" :
ra_num = []
ra_num.append(Rational( 1 , 2 ))
ra_num.append(Rational( 2 , 3 ))
ra_num.append(Rational( 3 , 4 ))
ra_num.append(Rational( 4 , 5 ))
index_max = maxRational(ra_num)
print ( str (ra_num[index_max].nume) + " " +
str (ra_num[index_max].deno))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Rational
{
public int nume, deno;
public Rational( int nume, int deno)
{
this .nume = nume;
this .deno = deno;
}
};
static int lcmOfDenominator(List<Rational> ra_num)
{
int lcm = ra_num[0].deno;
int i;
for (i = 1; i < ra_num.Count; i++)
lcm = (lcm * (ra_num[i].deno)) /
__gcd(lcm, ra_num[i].deno);
return lcm;
}
static int maxRational(List<Rational> ra_num)
{
int []temp = new int [ra_num.Count];
int lcm = lcmOfDenominator(ra_num);
int maximum = 0;
int maximumind = 0;
for ( int i = 0; i < ra_num.Count; i++)
{
temp[i] = (ra_num[i].nume) *
(lcm / ra_num[i].deno);
if (maximum < temp[i])
{
maximum = temp[i];
maximumind = i;
}
}
return maximumind;
}
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
public static void Main(String[] args)
{
List<Rational> ra_num = new List<Rational>();
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 ));
int index_max = maxRational(ra_num);
Console.WriteLine(ra_num[index_max].nume +
" " + ra_num[index_max].deno);
}
}
|
Javascript
<script>
class Rational {
constructor(nume, deno) {
this .nume = nume;
this .deno = deno;
}
}
function lcmOfDenominator(ra_num) {
var lcm = ra_num[0].deno;
var i;
for (i = 1; i < ra_num.length; i++)
lcm = (lcm * ra_num[i].deno) / __gcd(lcm, ra_num[i].deno);
return lcm;
}
function maxRational(ra_num) {
var temp = new Array(ra_num.Count);
var lcm = lcmOfDenominator(ra_num);
var maximum = 0;
var maximumind = 0;
for ( var i = 0; i < ra_num.length; i++) {
temp[i] = ra_num[i].nume * (lcm / ra_num[i].deno);
if (maximum < temp[i]) {
maximum = temp[i];
maximumind = i;
}
}
return maximumind;
}
function __gcd(a, b) {
if (b === 0) return a;
return __gcd(b, a % b);
}
var ra_num = [];
ra_num.push( new Rational(1, 2));
ra_num.push( new Rational(2, 3));
ra_num.push( new Rational(3, 4));
ra_num.push( new Rational(4, 5));
var index_max = maxRational(ra_num);
document.write(ra_num[index_max].nume + " " + ra_num[index_max].deno);
</script>
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...