Check if two given Rational Numbers are equal or not
Given two strings S and T representing non-negative rational numbers, the task is to check if the values of S and T are equal or not. If found to be true, then print “YES”. Otherwise, print “NO”.
Note: Any rational number can be represented in one of the following three ways:
- <IntegerPart> (e.g. 0, 12, 123)
- <IntegerPart><.><NonRepeatingPart> (e.g. 0.5, 1., 2.12, 2.0001)
- <IntegerPart><.><NonRepeatingPart><(><RepeatingPart><)> (e.g. 0.1(6), 0.9(9), 0.00(1212))
Examples:
Input: S = “0.(52)”, T = “0.5(25)”
Output: YES
Explanation:
The rational number “0.(52)” can be represented as 0.52525252…
The rational number “0.5(25)” can be represented as 0.525252525….
Therefore, the required output is “YES”.Input: S = “0.9(9)”, T = “1.”
Output: YES
Explanation:
The rational number “0.9(9)” can be represented as 0.999999999…, it is equal to 1.
The rational number “1.” can be represented as the number 1.
Therefore, the required output is “YES”.
Approach: The idea is to convert the rational numbers into fractions and then check if fractions of both the rational numbers are equal or not. If found to be true, then print “YES”. Otherwise, print “NO”. Following are the observations:
The repeating portion of a decimal expansion is conventionally denoted within a pair of round brackets.
For example: 1 / 6 = 0.16666666… = 0.1(6) = 0.1666(6) = 0.166(66)
Both 0.1(6) or 0.1666(6) or 0.166(66) are correct representations of 1 / 6.
Any rational numbers can be converted into fractions based on the following observations:
Let x = 0.5(25) —> (1)
Integer part = 0, Non-repeating part = 5, Repeating part = 25
Multiply both sides of equation (1) by 10 raised to the power of length of non-repeating part, i.e. 10 * x = 5.(25) —> (2)
Multiply both sides of equation (1) by 10 raised to the power of (length of non-repeating part + length of repeating part), 1000 * x = 525.(25) —> (3)
Subtract equation (2) from equation (3)
1000 * x – 10 * x = 525.(25)-5.(25)
990 * x = 520
⇒ x = 520 / 990
Follow the steps below to solve the problem:
- Convert both the rational numbers into fractions using the above observations.
- Check if both the fractions of the numbers are equal or not. If found to be true, then print “YES”.
- Otherwise, print “NO”.
Below is the implementation of the above approach:
Java
// Java program to implement // the above approach import java.io.*; import java.util.*; class GFG { // Function to check if the string S and T // are equal or not public static boolean isRationalEqual(String s, String t) { // Stores the fractional part of s Fraction f1 = Rational.parse(s).toFraction(); // Stores the fractional part of t Fraction f2 = Rational.parse(t).toFraction(); // If the condition satisfies, returns true // otherwise return false return f1.p * f2.q == f2.p * f1.q; } // Rational class having integer, non-repeating // and repeating part of the number public static class Rational { private final String integer, nonRepeating, repeating; // Constructor function to initialize // the object of the class private Rational(String integer, String nonRepeating, String repeating) { // Stores integer part this .integer = integer; // Stores non repeating part this .nonRepeating = nonRepeating; // Stores repeating part this .repeating = repeating; } // Function to split the string into // integer, repeating & non-repeating part public static Rational parse(String s) { // Split s into parts String[] parts = s.split( "[.()]" ); return new Rational( parts.length >= 1 ? parts[ 0 ] : "" , parts.length >= 2 ? parts[ 1 ] : "" , parts.length >= 3 ? parts[ 2 ] : "" ); } // Function to convert the string // into fraction public Fraction toFraction() { long a = tenpow(nonRepeating.length()); long i = Long.parseLong(integer + nonRepeating); // If there is no repeating part, then // form a new fraction of the form i/a if (repeating.length() == 0 ) { return new Fraction(i, a); } // Otherwise else { long b = tenpow(nonRepeating.length() + repeating.length()); long j = Long.parseLong( integer + nonRepeating + repeating); // Form the new Fraction and return return new Fraction(j - i, b - a); } } public String toString() { return String.format( "%s.%s(%s)" , integer, nonRepeating, repeating); } } // Fraction class having numerator as p // and denominator as q public static class Fraction { private final long p, q; // Constructor function to initialize // the object of the class private Fraction( long p, long q) { this .p = p; this .q = q; } public String toString() { return String.format( "%d/%d" , p, q); } } // Function to find 10 raised // to power of x public static long tenpow( int x) { assert x >= 0 ; long r = 1 ; while (--x >= 0 ) { r *= 10 ; } return r; } // Driver Code public static void main(String[] args) { // Given S and T String S = "0.(52)" , T = "0.5(25)" ; // Function Call if (isRationalEqual(S, T)) { System.out.println( "YES" ); } else { System.out.println( "NO" ); } // Print result } } |
YES
Time Complexity: O(N), where N is the maximum length of S and T
Auxiliary Space: O(1)