Find max of two Rational numbers
Given two Rational numbers, the task is to find the maximum of given two rational numbers.
Examples :
Input : first = 3/4, second= 3/2
Output : 3/2
Input : first = 100/100, second = 300/400
Output : 100/100
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 first = 3/2, second = 3/4
- First take a LCM of (4, 2) which is denominator of rational number. so the LCM of this is 4, then divide with both denominator and multiple with numerator of first and second respectively so the denominator value is first numerator = 6, second numerator = 3.
- Then find the max between these two. so here first numerator is max then print first rational number.
C++
#include <bits/stdc++.h>
using namespace std;
struct Rational
{
int nume, deno;
};
int lcm( int a, int b)
{
return (a * b) / (__gcd(a, b));
}
Rational maxRational(Rational first, Rational sec)
{
int k = lcm(first.deno, sec.deno);
int nume1 = first.nume;
int nume2 = sec.nume;
nume1 *= k / (first.deno);
nume2 *= k / (sec.deno);
return (nume2 < nume1)? first : sec;
}
int main()
{
Rational first = { 3, 2 };
Rational sec = { 3, 4 };
Rational res = maxRational(first, sec);
cout << res.nume << "/" << res.deno;
return 0;
}
|
Java
class GFG
{
static class Rational
{
int nume, deno;
public Rational( int nume, int deno)
{
this .nume = nume;
this .deno = deno;
}
};
static int lcm( int a, int b)
{
return (a * b) / (__gcd(a, b));
}
static Rational maxRational(Rational first, Rational sec)
{
int k = lcm(first.deno, sec.deno);
int nume1 = first.nume;
int nume2 = sec.nume;
nume1 *= k / (first.deno);
nume2 *= k / (sec.deno);
return (nume2 < nume1)? first : sec;
}
static int __gcd( int a, int b)
{
return b == 0 ? a:__gcd(b, a % b);
}
public static void main(String[] args)
{
Rational first = new Rational( 3 , 2 );
Rational sec = new Rational( 3 , 4 );
Rational res = maxRational(first, sec);
System.out.print(res.nume+ "/" + res.deno);
}
}
|
Python
import math
def lcm(a, b):
return (a * b) / / (math.gcd(a, b))
def maxRational(first, sec):
k = lcm(first[ 1 ], sec[ 1 ])
nume1 = first[ 0 ]
nume2 = sec[ 0 ]
nume1 * = k / / (first[ 1 ])
nume2 * = k / / (sec[ 1 ])
return first if (nume2 < nume1) else sec
first = [ 3 , 2 ]
sec = [ 3 , 4 ]
res = maxRational(first, sec)
print (res[ 0 ], "/" , res[ 1 ], sep = "")
|
C#
using System;
class GFG
{
class Rational
{
public int nume, deno;
public Rational( int nume, int deno)
{
this .nume = nume;
this .deno = deno;
}
};
static int lcm( int a, int b)
{
return (a * b) / (__gcd(a, b));
}
static Rational maxRational(Rational first, Rational sec)
{
int k = lcm(first.deno, sec.deno);
int nume1 = first.nume;
int nume2 = sec.nume;
nume1 *= k / (first.deno);
nume2 *= k / (sec.deno);
return (nume2 < nume1)? first : sec;
}
static int __gcd( int a, int b)
{
return b == 0 ? a:__gcd(b, a % b);
}
public static void Main(String[] args)
{
Rational first = new Rational(3, 2);
Rational sec = new Rational(3, 4);
Rational res = maxRational(first, sec);
Console.Write(res.nume + "/" + res.deno);
}
}
|
Javascript
<script>
class Rational
{
constructor(nume,deno)
{
this .nume = nume;
this .deno = deno;
}
}
function lcm(a,b)
{
return (a * b) / (__gcd(a, b));
}
function maxRational(first,sec)
{
let k = lcm(first.deno, sec.deno);
let nume1 = first.nume;
let nume2 = sec.nume;
nume1 *= k / (first.deno);
nume2 *= k / (sec.deno);
return (nume2 < nume1)? first : sec;
}
function __gcd(a,b)
{
return b == 0 ? a:__gcd(b, a % b);
}
let first = new Rational(3, 2 );
let sec = new Rational(3, 4 );
let res = maxRational(first, sec);
document.write(res.nume+ "/" + res.deno);
</script>
|
Time Complexity: O(log(max(deno,nume)))
Auxiliary Space: O(1)
An approach that works in constant time:
Let two rational number
First rational number => a / b –(i)
Second rational number => c / d –(ii)
Lets assume First rational number is greater than second rational number
=> a/b > c/d –(iii)
Multiplying with (d*b) on the both side of inequality => a*d > c*b –(iv)
Equation (iv) shows that if first rational number is greater than second than it will always hold the (iv) enquality.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Rational
{
int nume, deno;
};
Rational maxRational(Rational first, Rational second){
int a= first.nume;
int b= first.deno;
int c= second.nume;
int d= second.deno;
if ((a*d) > (b*c)) return first;
else return second;
}
int main(){
Rational first = {3, 4};
Rational second = {3, 2};
Rational res = maxRational(first, second);
cout << res.nume << "/" << res.deno;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static class Rational {
int nume, deno;
Rational( int num, int den)
{
nume = num;
deno = den;
}
};
static Rational maxRational(Rational first,
Rational second)
{
int a = first.nume;
int b = first.deno;
int c = second.nume;
int d = second.deno;
if ((a * d) > (b * c))
return first;
else
return second;
}
public static void main(String[] args)
{
Rational first = new Rational( 3 , 4 );
Rational second = new Rational( 3 , 2 );
Rational res = maxRational(first, second);
System.out.println(res.nume + "/" + res.deno);
}
}
|
Python3
import math
def maxRational(first, second):
a = first[ 0 ];
b = first[ 1 ];
c = second[ 0 ];
d = second[ 1 ];
if ((a * d) > (b * c)):
return first;
else :
return second;
first = [ 3 , 4 ];
second = [ 3 , 2 ];
res = maxRational(first, second);
print (res[ 0 ], "/" ,res[ 1 ]);
|
C#
using System;
class GFG {
class Rational
{
public int nume, deno;
public Rational( int nume, int deno)
{
this .nume = nume;
this .deno = deno;
}
}
static Rational maxRational(Rational first, Rational second){
int a= first.nume;
int b= first.deno;
int c= second.nume;
int d= second.deno;
if ((a*d) > (b*c))
return first;
else
return second;
}
public static void Main(String[] args)
{
Rational first = new Rational(3, 4);
Rational second = new Rational(3, 2);
Rational res = maxRational(first, second);
Console.Write(res.nume + "/" + res.deno);
}
}
|
Javascript
class Rational{
constructor(num, den){
this .nume = num;
this .deno = den;
}
}
function maxRational(first, second){
let a = first.nume;
let b = first.deno;
let c = second.first;
let d = second.deno;
if ((a*d) > (b*c)) return first;
else return second;
}
let first = new Rational(3,4);
let second = new Rational(3,2);
let res = maxRational(first, second);
document.write(res.nume + "/" + res.deno);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Please suggest if someone has a better solution that is more efficient in terms of space and time.
Last Updated :
24 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...