Given three integers a, b, and c representing a linear equation of the form ax + by = c, the task is to find the solution (x, y) of the given equation such that (x + y) is minimised. If no solution exists for the above equation then print “-1”.
Note: x and y are positive integers.
Input: a = 2, b = 2, c = 0
The given equation is 2x + 2y = 0.
Therefore, x = 0 and y = 0 is the required solution with minimum value of (x + y).
Input: a = 2, b = 2, c = 1
The given equation is 2x + 2y = 1.
No solution exists for the given equation for positive values of x and y.
Approach: To solve the above problem, find any solution say (x, y) of the given Linear Diophantine Equation and then accordingly find value of x and to minimised the sum.
Below is the solution (x’, y’) of the given equation:
where g is gcd(a, b) and k is any integer.
From the above equation we observe that:
- If a is less than b, we need to select the smallest possible value of K.
- Else, if a is greater than b, we need to select the largest possible value of K.
- If a = b, all solutions will have the same sum (x + y).
Below is the implementation of the above approach:
- Smallest positive integer X satisfying the given equation
- Count of Ordered Pairs (X, Y) satisfying the Equation 1/X + 1/Y = 1/N
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Split N as the sum of K numbers satisfying the given conditions
- Find x and y satisfying ax + by = n
- Find the missing value from the given equation a + b = c
- Boundary Value Analysis : Nature of Roots of a Quadratic equation
- Find count of numbers from 0 to n which satisfies the given equation for a value K
- Least root of given quadratic equation for value greater than equal to K
- Check if elements of an array can be arranged satisfying the given condition
- Count valid pairs in the array satisfying given conditions
- Largest sub-set possible for an array satisfying the given condition
- Generate N integers satisfying the given conditions
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Count of pairs satisfying the given condition
- Find the number of unique pairs satisfying given conditions
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Absolute difference between sum and product of roots of a quartic equation
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.