Given the values x, y, p, q of a simple chemical equation of the type:

The task is to find the values of constants b1, b2, b3 such that the equation is balanced on both sides and it must be the reduced form.
Examples:
Input: x = 2, y = 3, p = 4, q = 5
Output: b1 = 6, b2 = 5, b3 = 3
Input: x = 1, y = 2, p = 3, q = 1
Output: b1 = 6, b2 = 1, b3 = 2
Approach:
1. Check if p % x = 0 and q % y = 0 or not.
2. If yes, then we can simply say that
b1 = p / x,
b2 = q / y, and
b3 = 1
3. Else we need to use gcd to compute b1, b2, b3. We need the reduced form so gcd can help with it.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void balance( int x, int y, int p, int q)
{
int b1, b2, b3;
if (p % x == 0 && q % y == 0) {
b1 = p / x;
b2 = q / y;
b3 = 1;
}
else {
p = p * y;
q = q * x;
b3 = x * y;
int temp = gcd(p, gcd(q, b3));
b1 = p / temp;
b2 = q / temp;
b3 = b3 / temp;
}
cout << b1 << " " << b2
<< " " << b3 << endl;
}
int main()
{
int x = 2, y = 3, p = 4, q = 5;
balance(x, y, p, q);
}
|
Java
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static void balance( int x, int y, int p, int q)
{
int b1, b2, b3;
if (p % x == 0 && q % y == 0 ) {
b1 = p / x;
b2 = q / y;
b3 = 1 ;
}
else {
p = p * y;
q = q * x;
b3 = x * y;
int temp = gcd(p, gcd(q, b3));
b1 = p / temp;
b2 = q / temp;
b3 = b3 / temp;
}
System.out.print(b1 + " " + b2
+ " " + b3 + "\n" );
}
public static void main(String[] args)
{
int x = 2 , y = 3 , p = 4 , q = 5 ;
balance(x, y, p, q);
}
}
|
Python3
def gcd(a,b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def balance(x, y, p, q):
if (p % x = = 0 and q % y = = 0 ):
b1 = p / / x
b2 = q / / y
b3 = 1
else :
p = p * y
q = q * x
b3 = x * y
temp = gcd(p, gcd(q, b3))
b1 = p / / temp
b2 = q / / temp
b3 = b3 / / temp
print (b1,b2,b3)
if __name__ = = '__main__' :
x = 2
y = 3
p = 4
q = 5
balance(x, y, p, q)
|
C#
using System;
public class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void balance( int x, int y, int p, int q)
{
int b1, b2, b3;
if (p % x == 0 && q % y == 0) {
b1 = p / x;
b2 = q / y;
b3 = 1;
}
else {
p = p * y;
q = q * x;
b3 = x * y;
int temp = gcd(p, gcd(q, b3));
b1 = p / temp;
b2 = q / temp;
b3 = b3 / temp;
}
Console.Write(b1 + " " + b2
+ " " + b3 + "\n" );
}
public static void Main(String[] args)
{
int x = 2, y = 3, p = 4, q = 5;
balance(x, y, p, q);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function balance(x, y, p, q)
{
let b1, b2, b3;
if (p % x == 0 && q % y == 0) {
b1 = p / x;
b2 = q / y;
b3 = 1;
}
else {
p = p * y;
q = q * x;
b3 = x * y;
let temp = gcd(p, gcd(q, b3));
b1 = p / temp;
b2 = q / temp;
b3 = b3 / temp;
}
document.write(b1 + " " + b2
+ " " + b3 + "\n" );
}
let x = 2, y = 3, p = 4, q = 5;
balance(x, y, p, q);
</script>
|
Time Complexity: O(log(max(a, b)))
Auxiliary Space: O(log(max(a, b)))