Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmFind the number which when added to the given ratio a :...

Find the number which when added to the given ratio a : b, the ratio changes to c : d

Given four integers a, b, c and d. The task is to find the number X which when added to the numbers a and b the ratio changes from a : b to c : d.
Examples: 
 

Input: a = 3, b = 6, c = 3, d = 4 
Output:
When 6 is added to a and b 
a = 3 + 6 = 9 
b = 6 + 6 = 12 
And, the new ratio will be 9 : 12 = 3 : 4
Input: a = 2, b = 3, c = 4, d = 5 
Output:
 

 

Approach: Old ratio is a : b and new ratio is c : d. Let the required number be X
So, (a + X) / (b + X) = c / d 
or, ad + dx = bc + cx 
or, x(d – c) = bc – ad 
So, x = (bc – ad) / (d – c) 
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the
// required number X
int getX(int a, int b, int c, int d)
{
    int X = (b * c - a * d) / (d - c);
    return X;
}
 
// Driver code
int main()
{
    int a = 2, b = 3, c = 4, d = 5;
 
    cout << getX(a, b, c, d);
 
    return 0;
}


Java




// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to return the
// required number X
static int getX(int a, int b, int c, int d)
{
    int X = (b * c - a * d) / (d - c);
    return X;
}
 
// Driver code
public static void main (String[] args)
{
 
    int a = 2, b = 3, c = 4, d = 5;
    System.out.println (getX(a, b, c, d));
 
}
}
 
// The code is contributed by ajit..@23


Python




# Python3 implementation of the approach
 
# Function to return the
# required number X
def getX(a, b, c, d):
 
    X = (b * c - a * d) // (d - c)
    return X
 
# Driver code
 
a = 2
b = 3
c = 4
d = 5
 
print(getX(a, b, c, d))
 
# This code is contributed by mohit kumar 29


C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the
// required number X
static int getX(int a, int b, int c, int d)
{
    int X = (b * c - a * d) / (d - c);
    return X;
}
 
// Driver code
static public void Main ()
{
    int a = 2, b = 3, c = 4, d = 5;
    Console.Write(getX(a, b, c, d));
}
}
 
// The code is contributed by Tushil.


Javascript




<script>
// javascript implementation of the approach
   
// Function to return the
// required number X
function getX(a , b , c , d)
{
    var X = (b * c - a * d) / (d - c);
    return X;
}
 
// Driver code
 
var a = 2, b = 3, c = 4, d = 5;
document.write(getX(a, b, c, d));
 
// This code is contributed by 29AjayKumar
</script>


Output: 

2

 

Time Complexity: O(1), since there is a basic arithmetic operation that takes constant time.

Auxiliary Space: O(1), since no extra space has been taken.

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!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments