Given a, b and n. Find x and y that satisfies ax + by = n. Print any of the x and y satisfying the equation
Examples :
Input : n=7 a=2 b=3
Output : x=2, y=1
Explanation: here x and y satisfies the equation
Input : 4 2 7
Output : No Solution Exists
We can check if any solutions exists or not using Linear Diophantine Equations, but here we need to find out the solutions for this equation, we can simply iterate for all possible values from 0 to n as it cannot exceed n for this given equation. So solving this equation with pen and paper gives y=(n-ax)/b and similarly we get the other number to be x=(n-by)/a. But this way we would get only positive solution for x and y may be positive or negative depends on sign of b.
using Linear Diophantine Equation, we can say “no solution” only when GCD(a, b) would not be a divisor of n. otherwise, solution exists.
C++14
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b, int &x, int &y){
if (b == 0){
x= 1; y = 0;
return a;
}
int x1, y1;
int d = gcd(b, a%b, x1, y1);
x = y1;
y = x1 - y1*(a/b);
return d;
}
void solution( int a, int b, int n)
{
int x0, y0;
int g = gcd(a, b, x0, y0);
if (n%g != 0){
cout<< "No Solution Exists" <<endl;
return ;
}
x0 = x0*n/g;
y0 = y0*n/g;
cout<< "x = " <<x0<< ", y = " <<y0<<endl;
for ( int k = -3; k <= 3; k++){
int x = x0 + k*(b/g);
int y = y0 - k*(a/g);
cout<< "x = " <<x<< ", y = " <<y<<endl;
}
}
int main()
{
int a = 2, b = 3, n = 7;
solution(a, b, n);
return 0;
}
|
Java
import java.io.*;
class GfG {
static void solution( int a, int b, int n)
{
for ( int i = 0 ; i * a <= n; i++)
{
if ((n - (i * a)) % b == 0 )
{
System.out.println( "x = " + i +
", y = " +
(n - (i * a)) / b);
return ;
}
}
System.out.println( "No solution" );
}
public static void main (String[] args)
{
int a = 2 , b = 3 , n = 7 ;
solution(a, b, n);
}
}
|
Python3
def solution (a, b, n):
i = 0
while i * a < = n:
if (n - (i * a)) % b = = 0 :
print ( "x = " ,i , ", y = " ,
int ((n - (i * a)) / b))
return 0
i = i + 1
print ( "No solution" )
a = 2
b = 3
n = 7
solution(a, b, n)
|
C#
using System;
class GfG {
static void solution( int a, int b, int n)
{
for ( int i = 0; i * a <= n; i++)
{
if ((n - (i * a)) % b == 0)
{
Console.Write( "x = " + i +
", y = " +
(n - (i * a)) / b);
return ;
}
}
Console.Write( "No solution" );
}
public static void Main ()
{
int a = 2, b = 3, n = 7;
solution(a, b, n);
}
}
|
PHP
<?php
function solution( $a , $b , $n )
{
for ( $i = 0; $i * $a <= $n ; $i ++)
{
if (( $n - ( $i * $a )) % $b == 0)
{
echo "x = " , $i , ", y = " ,
( $n - ( $i * $a )) / $b ;
return ;
}
}
echo "No solution" ;
}
$a = 2; $b = 3; $n = 7;
solution( $a , $b , $n );
?>
|
Javascript
<script>
function solution(a, b, n)
{
for (let i = 0; i * a <= n; i++)
{
if ((n - (i * a)) % b == 0)
{
document.write( "x = " + i +
", y = " +
(n - (i * a)) / b);
return ;
}
}
document.write( "No solution" );
}
let a = 2, b = 3, n = 7;
solution(a, b, n);
</script>
|
Outputx = -7, y = 7
x = -16, y = 13
x = -13, y = 11
x = -10, y = 9
x = -7, y = 7
x = -4, y = 5
x = -1, y = 3
x = 2, y = 1
Time Complexity: O(log(min(a, b))), as we are using a Euclid gcd function.
Auxiliary Space: O(1), since no extra space has been taken.
The above written code is not handling base cases.
when n=0, a= 0, b= 0 [ ans = infinite solution ]
when n != 0, a = 0 , b = 0 [ ans = no solution ]
when n = 0, a != 0 , b!=0 [ ans = one solution exists ]