![]() |
VOOZH | about |
Given a string s containing two fractions a/b and c/d, compare them and return the greater. If they are equal, then return "equal".
Note: The string s contains "a/b, c/d"(fractions are separated by comma(,) & space( )).
Examples :
Input: s = "5/6, 11/45"
Output: 5/6
Explanation: 5/6=0.8333 and 11/45=0.2444, So 5/6 is a greater fraction.
Input: s= "8/1, 8/1"
Output: equal
Explanation: We can see that both the fractions are same, so we'll return a string "equal".
Table of Content
The idea is to convert both fractions into decimal values and compare them.
5/6
Time Complexity: O(1)
Auxiliary Space: O(1)
The idea is to compare fractions using cross multiplication instead of division.
For two fractions
a/bandc/d, compare a * dβ βandβ βb * c
- If
a * d > b * c, then the first fraction is greater.- If
a * d < b * c, then the second fraction is greater.- Otherwise, both fractions are equal.
5/6
Time Complexity: O(1)
Auxiliary Space: O(1)