Solving a Linear Diophantine Equationsee description for examples
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A linear Diophantine equation has the form ax + by = c, where a, b, and c are integers and the goal is to find integer values of x and y. The problem is simple to state, but the solution depends on one precise condition involving the greatest common divisor.
The standard tool is the extended Euclidean algorithm. Once you know one solution, you automatically know all integer solutions.
Check Whether a Solution Exists
The equation
has an integer solution if and only if gcd(a, b) divides c.
That is the first thing to test. If gcd(a, b) does not divide c, stop immediately because no integer solution exists.
For example:
- '
6x + 9y = 12has solutions becausegcd(6, 9) = 3and3divides12' - '
6x + 9y = 10has no solutions because3does not divide10'
Use the Extended Euclidean Algorithm
The extended Euclidean algorithm finds integers s and t such that:
Once you have those coefficients, scale them by c / gcd(a, b) to get one particular solution to ax + by = c.
Here is a compact Python implementation:
This returns one valid integer pair together with the gcd.
Describe All Integer Solutions
If (x0, y0) is one solution and g = gcd(a, b), then every integer solution is:
where k is any integer.
That formula matters because Diophantine equations usually have either no solutions or infinitely many integer solutions.
Worked Example
Solve:
First, gcd(6, 9) = 3, and 3 divides 12, so solutions exist.
One relation from the extended Euclidean algorithm is:
Multiply both sides by 4:
So one particular solution is:
Now apply the general formula:
for any integer k.
When You Need Positive Solutions
Sometimes the problem asks for non-negative or positive solutions only. In that case, the general solution is still the right starting point, but you must choose k so both variables land in the required range.
That turns the problem into a small inequality exercise instead of a new number-theory method.
Common Pitfalls
- Forgetting to check whether
gcd(a, b)dividescbefore searching for solutions. - Finding one solution and stopping even though the full family of solutions is required.
- Mixing up the signs in the general solution formula.
- Assuming every Diophantine equation has a positive solution just because it has an integer solution.
- Trying random substitutions instead of using the extended Euclidean algorithm directly.
Summary
- A linear Diophantine equation
ax + by = chas integer solutions exactly whengcd(a, b)dividesc. - The extended Euclidean algorithm gives one particular solution efficiently.
- Once one solution is known, all solutions follow from a simple parameter
k. - For restricted solutions such as non-negative integers, apply inequalities to the general form.
- The key step is arithmetic structure, not brute-force searching.
Related reading
- Solving linear equations represented as a string
- Sort a set of 3-D points in clockwise/counter-clockwise order
- Sort Four Points in Clockwise Order
- Sort points in clockwise order?
- Sort polygon's points for drawing
- Sorted intervals query
- Sorting a permutation with minimum cost
- Sorting algorithm to implement highest total combinations

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.