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:
- Find x and y satisfying ax + by = n
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Largest sub-set possible for an array satisfying the given condition
- Count of pairs 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
- Check if elements of an array can be arranged satisfying the given condition
- Find the number of unique pairs satisfying given conditions
- Count valid pairs in the array satisfying given conditions
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Number of solutions for the equation x + y + z <= n
- Find the missing value from the given equation a + b = c
- Sort an array after applying the given equation
- Check whether one root of the Quadratic Equation is twice of other or not
- Number of integral solutions of the equation x1 + x2 +.... + xN = k
- Print values of 'a' in equation (a+b) <= n and a+b is divisible by x
- Find the number of solutions to the given equation
- Find the quadratic equation from the given roots
- Satisfy the parabola when point (A, B) and the equation is given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.