Given a fraction in form of a/b where a & b are positive integers. Find ?X such that when it is added to numerator as well as denominator of given fraction it will result into a new Ir-reducible fraction c/d.
Examples:
Input : a = 4, b = 10, c = 1, d = 2 Output : ?X = 2 Explanation : (a + ?X)/(b + ?X) = (4 + 2) / (10 + 2) = 6/12 = 1/2 = c/d Input : a = 4, b = 10, c = 2, d = 5 Output : ?X = 0 Explanation : (a + ?X) / (b + ?X) = (4) / (10) = 2 / 5 = c/d
To solve this type of problem rather than implementing just a programming approach we have to do a little of mathematics. mathematics just necessary is as:
As per question we have to find ?X such that: => (a + ?X) / (b + ?X) = c / d => ad + d?X = bc + c?X => d?X - c?X = bc - ad => ?X (d - c) = bc -ad => ?X = (bc - ad) / (d - c)
So, in a way to finding ?X we have to only calculate .
C++
// C++ Program to find deltaX #include <bits/stdc++.h> using namespace std; // function to find delta X int findDelta( int a, int b, int c, int d) { return (b * c - a * d) / (d - c); } // driver program int main() { int a = 3, b = 9, c = 3, d = 5; // u0394X is code for delta sign cout << "\u0394X = " << findDelta(a, b, c, d); return 0; } |
Java
// Java Program to // find deltaX import java.io.*; class GFG { // function to find delta X static int findDelta( int a, int b, int c, int d) { return (b * c - a * d) / (d - c); } // Driver Code public static void main(String args[]) { int a = 3 , b = 9 , c = 3 , d = 5 ; // u0394X is code // for delta sign System.out.print( "\u0394X = " + findDelta(a, b, c, d)); } } // This code is contributed // by Manish Shaw(manishshaw1) |
Python3
# Python Program to find deltaX # !/usr/bin/python # coding=utf-8 # function to find delta X def findDelta(a, b, c, d) : return int ((b * c - a * d) / (d - c)); # Driver Code a = 3 ; b = 9 ; c = 3 ; d = 5 ; # u0394X is code # for delta sign print ( "X = {}" . format (findDelta(a, b, c, d))); # This code is contributed by # Manish Shaw(manishshaw1) |
C#
// C# Program to // find deltaX using System; class GFG { // function to find delta X static int findDelta( int a, int b, int c, int d) { return (b * c - a * d) / (d - c); } // Driver Code static void Main() { int a = 3, b = 9, c = 3, d = 5; // u0394X is code // for delta sign Console.Write( "\u0394X = " + findDelta(a, b, c, d)); } } // This code is contributed // by Manish Shaw(manishshaw1) |
PHP
<?php // PHP Program to find deltaX // function to find delta X function findDelta( $a , $b , $c , $d ) { return ( $b * $c - $a * $d ) / ( $d - $c ); } // Driver Code $a = 3; $b = 9; $c = 3; $d = 5; // u0394X is code for delta sign echo "?X = " .findDelta( $a , $b , $c , $d ); // This code is contributed by // Manish Shaw(manishshaw1) ?> |
Javascript
<script> // JavaScript Program to // find deltaX // function to find delta X function findDelta(a, b, c, d) { return (b * c - a * d) / (d - c); } // Driver Code let a = 3, b = 9, c = 3, d = 5; // u0394X is code // for delta sign document.write( "\u0394X = " + findDelta(a, b, c, d)); // This code is contributed by splevel62. </script> |
Output:
?X = 6
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 neveropen!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!