Given two fractions a/b and c/d, compare them and print the larger of the two.
Examples :
Input: 5/6, 11/45
Output: 5/6
Input: 4/5 and 2/3
Output: 4/5
We can simply convert the fractions into floating-point values by dividing the numerator by the denominator. Once we have the two floating-point numbers corresponding to each fraction, we can compare these numbers and determine which fraction is larger.
However, the computed answer may be incorrect due to floating-point approximations and truncations during the division process. To get the most accurate answer we should avoid resorting to floating-point division.
To compare two fractions we need to make their denominators the same. We can do this by cross-multiplying numerators with denominators. Let’s see how this works
We have two fractions a/b and c/d.
Let Y = (a/b - c/d)
= (ad - bc)/(bd)
Now,
if Y > 0 then a/b > c/d
if Y = 0 then a/b = c/d
if Y < o then a/b < c/d
Since bd is always positive, the sign of Y depends only on the
numerator (ad-bc). So we need to compute (ad-bc) only.
Complexity :
Since we perform two multiplication and one subtraction operation, the answer is computed in constant time i.e O(1)
C++
#include <bits/stdc++.h>
using namespace std;
struct Fraction {
int num, den;
};
Fraction maxFraction(Fraction first, Fraction sec)
{
int a = first.num;
int b = first.den;
int c = sec.num;
int d = sec.den;
int Y = a * d - b * c;
return (Y > 0) ? first : sec;
}
int main()
{
Fraction first = { 3, 2 };
Fraction sec = { 3, 4 };
Fraction res = maxFraction(first, sec);
cout << res.num << "/" << res.den;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Fraction {
int num, den;
Fraction( int n, int d){
num=n;
den=d;
}
static Fraction maxFraction(Fraction first, Fraction sec)
{
int a = first.num;
int b = first.den;
int c = sec.num;
int d = sec.den;
int Y = a * d - b * c;
return (Y > 0 ) ? first : sec;
}
public static void main (String[] args) {
Fraction first = new Fraction( 3 , 2 );
Fraction sec = new Fraction( 3 , 4 );
Fraction res = maxFraction(first, sec);
System.out.println(res.num + "/" + res.den);
}
}
|
Python3
def maxFraction(first, sec):
a = first[ 0 ]; b = first[ 1 ]
c = sec[ 0 ]; d = sec[ 1 ]
Y = a * d - b * c
return first if Y else sec
first = ( 3 , 2 )
sec = ( 3 , 4 )
res = maxFraction(first, sec)
print ( str (res[ 0 ]) + "/" + str (res[ 1 ]))
|
C#
using System;
class Fraction {
int num, den;
Fraction( int n, int d)
{
num=n;
den=d;
}
static Fraction maxFraction(Fraction first, Fraction sec)
{
int a = first.num;
int b = first.den;
int c = sec.num;
int d = sec.den;
int Y = a * d - b * c;
return (Y > 0) ? first : sec;
}
public static void Main ()
{
Fraction first = new Fraction( 3, 2 );
Fraction sec = new Fraction( 3, 4 );
Fraction res = maxFraction(first, sec);
Console.WriteLine(res.num + "/" + res.den);
}
}
|
PHP
<?php
function maxFraction( $first , $sec )
{
$a = $first [0];
$b = $first [1];
$c = $sec [0];
$d = $sec [1];
$Y = $a * $d - $b * $c ;
return ( $Y ) ? $first : $sec ;
}
$first = array ( 3, 2 );
$sec = array ( 3, 4 );
$res = maxFraction( $first , $sec );
echo $res [0] . "/" . $res [1];
?>
|
Javascript
function maxFraction(first, sec) {
a = first[0]; b = first[1]
c = sec[0]; d = sec[1]
Y = a * d - b * c
return (Y > 0) ? first : sec;
}
first = [ 3, 2 ];
sec = [ 3, 4 ];
res = maxFraction(first, sec);
document.write(res[0] + "/" + res[1]);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Jan, 2023
Like Article
Save Article