Find initial integral solution of Linear Diophantine equation if finite solution exists
Last Updated :
03 May, 2022
Given three integers a , b , and c representing a linear equation of the form: ax + by = c . The task is to find the initial integral solution of the given equation if a finite solution exists.
A Linear Diophantine equation (LDE) is an equation with 2 or more integer unknowns and the integer unknowns are each to at most degree of 1. Linear Diophantine equation in two variables takes the form of ax+by=c, where x,y are integer variables and a, b, c are integer constants. x and y are unknown variables.
Examples:
Input: a = 4, b = 18, c = 10 Output: x = -20, y = 5
Explanation: (-20)*4 + (5)*18 = 10
Input: a = 9, b = 12, c = 5 Output: No Solutions exists
Approach:
First, check if a and b are non-zero.
If both of them are zero and c is non-zero then, no solution exists. If c is also zero then infinite solution exits.
For given a and b , calculate the value of x1 , y1 , and gcd using Extended Euclidean Algorithm .
Now, for a solution to existing gcd(a, b) should be multiple of c .
Calculate the solution of the equation as follows:
x = x1 * ( c / gcd )
y = y1 * ( c / gcd )
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd_extend(int a, int b,
int& x, int& y)
{
if (b == 0) {
x = 1;
y = 0;
return a;
}
else {
int g = gcd_extend(b,
a % b, x, y);
int x1 = x, y1 = y;
x = y1;
y = x1 - (a / b) * y1;
return g;
}
}
void print_solution(int a, int b, int c)
{
int x, y;
if (a == 0 && b == 0) {
if (c == 0) {
cout
<< "Infinite Solutions Exist"
<< endl;
}
else {
cout
<< "No Solution exists"
<< endl;
}
}
int gcd = gcd_extend(a, b, x, y);
if (c % gcd != 0) {
cout
<< "No Solution exists"
<< endl;
}
else {
cout << "x = " << x * (c / gcd)
<< ", y = " << y * (c / gcd)
<< endl;
}
}
int main(void)
{
int a, b, c;
a = 4;
b = 18;
c = 10;
print_solution(a, b, c);
return 0;
}
Java
class GFG{
static int x, y;
static int gcd_extend(int a, int b)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
int g = gcd_extend(b, a % b);
int x1 = x, y1 = y;
x = y1;
y = x1 - (a / b) * y1;
return g;
}
}
static void print_solution(int a, int b, int c)
{
if (a == 0 && b == 0)
{
if (c == 0)
{
System.out.print("Infinite Solutions " +
"Exist" + "\n");
}
else
{
System.out.print("No Solution exists" +
"\n");
}
}
int gcd = gcd_extend(a, b);
if (c % gcd != 0)
{
System.out.print("No Solution exists" + "\n");
}
else
{
System.out.print("x = " + x * (c / gcd) +
", y = " + y * (c / gcd) + "\n");
}
}
public static void main(String[] args)
{
int a, b, c;
a = 4;
b = 18;
c = 10;
print_solution(a, b, c);
}
}
Python3
import math
x, y = 0, 0
def gcd_extend(a, b):
global x, y
if (b == 0):
x = 1
y = 0
return a
else:
g = gcd_extend(b, a % b)
x1, y1 = x, y
x = y1
y = x1 - math.floor(a / b) * y1
return g
def print_solution(a, b, c):
if (a == 0 and b == 0):
if (c == 0):
print("Infinite Solutions Exist")
else :
print("No Solution exists")
gcd = gcd_extend(a, b)
if (c % gcd != 0):
print("No Solution exists")
else:
print("x = ", int(x * (c / gcd)), ", y = ", int(y * (c / gcd)), sep = "")
a = 4
b = 18
c = 10
print_solution(a, b, c)
C#
using System;
class GFG{
static int x, y;
static int gcd_extend(int a, int b)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
int g = gcd_extend(b, a % b);
int x1 = x, y1 = y;
x = y1;
y = x1 - (a / b) * y1;
return g;
}
}
static void print_solution(int a, int b, int c)
{
if (a == 0 && b == 0)
{
if (c == 0)
{
Console.Write("Infinite Solutions " +
"Exist" + "\n");
}
else
{
Console.Write("No Solution exists" +
"\n");
}
}
int gcd = gcd_extend(a, b);
if (c % gcd != 0)
{
Console.Write("No Solution exists" + "\n");
}
else
{
Console.Write("x = " + x * (c / gcd) +
", y = " + y * (c / gcd) + "\n");
}
}
public static void Main(String[] args)
{
int a, b, c;
a = 4;
b = 18;
c = 10;
print_solution(a, b, c);
}
}
Javascript
<script>
let x, y;
function gcd_extend(a, b)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
let g = gcd_extend(b, a % b);
let x1 = x, y1 = y;
x = y1;
y = x1 - Math.floor(a / b) * y1;
return g;
}
}
function print_solution(a, b, c)
{
if (a == 0 && b == 0)
{
if (c == 0)
{
document.write("Infinite Solutions " +
"Exist" + "\n");
}
else
{
document.write("No Solution exists" +
"\n");
}
}
let gcd = gcd_extend(a, b);
if (c % gcd != 0)
{
document.write("No Solution exists" + "\n");
}
else
{
document.write("x = " + x * (c / gcd) +
", y = " + y * (c / gcd) + "<br/>");
}
}
let a, b, c;
a = 4;
b = 18;
c = 10;
print_solution(a, b, c);
</script>
Time Complexity: O(log(max(A, B))) , where A and B are the coefficient of x and y in the given linear equation. Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...