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 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 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 value of x_{1}, y_{1}and*gcd*using Extended Euclidean Algorithm. - Now, for solution to exist gcd(a, b) should be multiple of
**c**. - Calculate solution of the equation as follows:
x = x

_{1}* ( c / gcd ) y = y_{1}* ( c / gcd )

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to implement the extended ` `// euclid algorithm ` `int` `gcd_extend(` `int` `a, ` `int` `b, ` ` ` `int` `& x, ` `int` `& y) ` `{ ` ` ` `// Base Case ` ` ` `if` `(b == 0) { ` ` ` `x = 1; ` ` ` `y = 0; ` ` ` `return` `a; ` ` ` `} ` ` ` ` ` `// Recursively find the gcd ` ` ` `else` `{ ` ` ` `int` `g = gcd_extend(b, ` ` ` `a % b, x, y); ` ` ` `int` `x1 = x, y1 = y; ` ` ` `x = y1; ` ` ` `y = x1 - (a / b) * y1; ` ` ` `return` `g; ` ` ` `} ` `} ` ` ` `// Function to print the solutions of ` `// the given equations ax + by = c ` `void` `print_solution(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `int` `x, y; ` ` ` `if` `(a == 0 && b == 0) { ` ` ` ` ` `// Condition for infinite solutions ` ` ` `if` `(c == 0) { ` ` ` `cout ` ` ` `<< ` `"Infinite Solutions Exist"` ` ` `<< endl; ` ` ` `} ` ` ` ` ` `// Condition for no solutions exist ` ` ` `else` `{ ` ` ` `cout ` ` ` `<< ` `"No Solution exists"` ` ` `<< endl; ` ` ` `} ` ` ` `} ` ` ` `int` `gcd = gcd_extend(a, b, x, y); ` ` ` ` ` `// Condition for no solutions exist ` ` ` `if` `(c % gcd != 0) { ` ` ` `cout ` ` ` `<< ` `"No Solution exists"` ` ` `<< endl; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// Print the solution ` ` ` `cout << ` `"x = "` `<< x * (c / gcd) ` ` ` `<< ` `", y = "` `<< y * (c / gcd) ` ` ` `<< endl; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main(` `void` `) ` `{ ` ` ` `int` `a, b, c; ` ` ` ` ` `// Given coefficients ` ` ` `a = 4; ` ` ` `b = 18; ` ` ` `c = 10; ` ` ` ` ` `// Function Call ` ` ` `print_solution(a, b, c); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `static` `int` `x, y; ` ` ` `// Function to implement the extended ` `// euclid algorithm ` `static` `int` `gcd_extend(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Base Case ` ` ` `if` `(b == ` `0` `) ` ` ` `{ ` ` ` `x = ` `1` `; ` ` ` `y = ` `0` `; ` ` ` `return` `a; ` ` ` `} ` ` ` ` ` `// Recursively find the gcd ` ` ` `else` ` ` `{ ` ` ` `int` `g = gcd_extend(b, a % b); ` ` ` `int` `x1 = x, y1 = y; ` ` ` `x = y1; ` ` ` `y = x1 - (a / b) * y1; ` ` ` `return` `g; ` ` ` `} ` `} ` ` ` `// Function to print the solutions of ` `// the given equations ax + by = c ` `static` `void` `print_solution(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `if` `(a == ` `0` `&& b == ` `0` `) ` ` ` `{ ` ` ` ` ` `// Condition for infinite solutions ` ` ` `if` `(c == ` `0` `) ` ` ` `{ ` ` ` `System.out.print(` `"Infinite Solutions "` `+ ` ` ` `"Exist"` `+ ` `"\n"` `); ` ` ` `} ` ` ` ` ` `// Condition for no solutions exist ` ` ` `else` ` ` `{ ` ` ` `System.out.print(` `"No Solution exists"` `+ ` ` ` `"\n"` `); ` ` ` `} ` ` ` `} ` ` ` `int` `gcd = gcd_extend(a, b); ` ` ` ` ` `// Condition for no solutions exist ` ` ` `if` `(c % gcd != ` `0` `) ` ` ` `{ ` ` ` `System.out.print(` `"No Solution exists"` `+ ` `"\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Print the solution ` ` ` `System.out.print(` `"x = "` `+ x * (c / gcd) + ` ` ` `", y = "` `+ y * (c / gcd) + ` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a, b, c; ` ` ` ` ` `// Given coefficients ` ` ` `a = ` `4` `; ` ` ` `b = ` `18` `; ` ` ` `c = ` `10` `; ` ` ` ` ` `// Function Call ` ` ` `print_solution(a, b, c); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `static` `int` `x, y; ` ` ` `// Function to implement the extended ` `// euclid algorithm ` `static` `int` `gcd_extend(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Base Case ` ` ` `if` `(b == 0) ` ` ` `{ ` ` ` `x = 1; ` ` ` `y = 0; ` ` ` `return` `a; ` ` ` `} ` ` ` ` ` `// Recursively find the gcd ` ` ` `else` ` ` `{ ` ` ` `int` `g = gcd_extend(b, a % b); ` ` ` `int` `x1 = x, y1 = y; ` ` ` `x = y1; ` ` ` `y = x1 - (a / b) * y1; ` ` ` `return` `g; ` ` ` `} ` `} ` ` ` `// Function to print the solutions of ` `// the given equations ax + by = c ` `static` `void` `print_solution(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` `if` `(a == 0 && b == 0) ` ` ` `{ ` ` ` ` ` `// Condition for infinite solutions ` ` ` `if` `(c == 0) ` ` ` `{ ` ` ` `Console.Write(` `"Infinite Solutions "` `+ ` ` ` `"Exist"` `+ ` `"\n"` `); ` ` ` `} ` ` ` ` ` `// Condition for no solutions exist ` ` ` `else` ` ` `{ ` ` ` `Console.Write(` `"No Solution exists"` `+ ` ` ` `"\n"` `); ` ` ` `} ` ` ` `} ` ` ` `int` `gcd = gcd_extend(a, b); ` ` ` ` ` `// Condition for no solutions exist ` ` ` `if` `(c % gcd != 0) ` ` ` `{ ` ` ` `Console.Write(` `"No Solution exists"` `+ ` `"\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Print the solution ` ` ` `Console.Write(` `"x = "` `+ x * (c / gcd) + ` ` ` `", y = "` `+ y * (c / gcd) + ` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `a, b, c; ` ` ` ` ` `// Given coefficients ` ` ` `a = 4; ` ` ` `b = 18; ` ` ` `c = 10; ` ` ` ` ` `// Function call ` ` ` `print_solution(a, b, c); ` `} ` `} ` ` ` `// This code contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

x = -20, y = 5

**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)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Linear Diophantine Equations
- Find the integral roots of a given Cubic equation
- Number of integral solutions of the equation x1 + x2 +.... + xN = k
- Number of non-negative integral solutions of sum equation
- Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c
- Find number of solutions of a linear equation of n variables
- Solve the Linear Equation of Single Variable
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Find initial sequence that produces a given Array by cyclic increments upto index P
- Find integral points with minimum distance from given set of integers using BFS
- Count of elements that can be deleted without disturbing the mean of the initial array
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Count Integral points inside a Triangle
- Number of Integral Points between Two Points
- Number of non-negative integral solutions of a + b + c = n
- Trapezoidal Rule for Approximate Value of Definite Integral
- Program for finding the Integral of a given function using Boole's Rule
- Count of integral coordinates that lies inside a Square

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.