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 minimized. If no solution exists for the above equation then print “-1”.
Note: x and y are positive integers.
Examples:
Input: a = 2, b = 2, c = 0
Output: 0
Explanation:
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
Output: -1
Explanation:
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:
[Tex]y’ = y – k * \frac{a}{g} [/Tex]
where g is gcd(a, b) and k is any integer.
[Tex]x’ + y’ = x + y + k*\frac{b-a}{g} [/Tex]
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:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the gcd of a & b // by Euclid's method // x and y store solution of // equation ax + by = g int gcd(int a, int b, int* x, int* y) { if (b == 0) { *x = 1; *y = 0; return a; } int x1, y1; int store_gcd = gcd(b, a % b, &x1;, &y1;); // Euclidean Algorithm *x = y1; *y = x1 - y1 * (a / b); // store_gcd returns // the gcd of a and b return store_gcd; } // Function to find any // possible solution int possible_solution(int a, int b, int c, int* x0, int* y0, int* g) { *g = gcd(fabs(a), fabs(b), x0, y0); // Condition if solution // does not exists if (c % *g) { return 0; } *x0 *= c / *g; *y0 *= c / *g; // Adjusting the sign // of x0 and y0 if (a < 0) *x0 *= -1; if (b < 0) *y0 *= -1; return 1; } // Function to shift solution void shift_solution(int* x, int* y, int a, int b, int shift_var) { // Shifting to obtain // another solution *x += shift_var * b; *y -= shift_var * a; } // Function to find minimum // value of x and y int find_min_sum(int a, int b, int c) { int x, y, g; // g is the gcd of a and b if (!possible_solution(a, b, c, &x;, &y;, &g;)) return -1; a /= g; b /= g; // Store sign of a and b int sign_a = a > 0 ? +1 : -1; int sign_b = b > 0 ? +1 : -1; shift_solution(&x;, &y;, a, b, -x / b); // If x is less than 0, then // shift solution if (x < 0) shift_solution(&x;, &y;, a, b, sign_b); int minx1 = x; shift_solution(&x;, &y;, a, b, y / a); // If y is less than 0, then // shift solution if (y < 0) shift_solution(&x;, &y;, a, b, -sign_a); int minx2 = x; if (minx2 > x) swap(minx2, x); int minx = max(minx1, minx2); // Find intersection such // that both x and y are positive if (minx > x) return -1; // miny is value of y // corresponding to minx int miny = (c - a * x) / b; // Returns minimum value of x+y return (miny + minx); } // Driver Code int main() { // Given a, b, and c int a = 2, b = 2, c = 0; // Function Call cout << find_min_sum(a, b, c) << "\n"; return 0; } |
Java
// Java program for the above approach import java.lang.*;class GFG{ public static int x = 0, y = 0, x1 = 0, y1 = 0;public static int x0 = 0, y0 = 0, g = 0;// Function to find the gcd of a & b // by Euclid's method // x and y store solution of // equation ax + by = g public static int gcd(int a, int b){ if (b == 0) { x = 1; y = 0; return a; } int store_gcd = gcd(b, a % b); // Euclidean Algorithm x = y1; y = x1 - y1 * (a / b); // store_gcd returns // the gcd of a and b return store_gcd;}// Function to find any // possible solution public static int possible_solution(int a, int b, int c){ g = gcd(Math.abs(a), Math.abs(b)); // Condition if solution // does not exists if (c % g != 0) { return 0; } x0 *= c / g; y0 *= c / g; // Adjusting the sign // of x0 and y0 if (a < 0) x0 *= -1; if (b < 0) y0 *= -1; return 1;}// Function to shift solutionpublic static void shift_solution(int a, int b, int shift_var){ // Shifting to obtain // another solution x += shift_var * b; y -= shift_var * a;}// Function to find minimum// value of x and ypublic static int find_min_sum(int a, int b, int c){ int x = 0, y = 0, g = 0; // g is the gcd of a and b if (possible_solution(a, b, c) == 0) return -1; if (g != 0) { a /= g; b /= g; } // Store sign of a and b int sign_a = a > 0 ? + 1 : -1; int sign_b = b > 0 ? + 1 : -1; shift_solution(a, b, -x / b); // If x is less than 0, then // shift solution if (x < 0) shift_solution(a, b, sign_b); int minx1 = x; shift_solution(a, b, y / a); // If y is less than 0, then // shift solution if (y < 0) shift_solution(a, b, -sign_a); int minx2 = x; if (minx2 > x) { int temp = minx2; minx2 = x; x = temp; } int minx = Math.max(minx1, minx2); // Find intersection such // that both x and y are positive if (minx > x) return -1; // miny is value of y // corresponding to minx int miny = (c - a * x) / b; // Returns minimum value of x+y return (miny + minx);}// Driver Codepublic static void main(String[] args){ // Given a, b, and c int a = 2, b = 2, c = 0; // Function call System.out.println(find_min_sum(a, b, c));}}// This code is contributed by grand_master |
C#
// C# program for the // above approachusing System;class GFG{public static int x = 0, y = 0, x1 = 0, y1 = 0;public static int x0 = 0, y0 = 0, g = 0;// Function to find the gcd // of a & b by Euclid's method// x and y store solution of// equation ax + by = gpublic static int gcd(int a, int b){ if (b == 0) { x = 1; y = 0; return a; } int store_gcd = gcd(b, a % b); // Euclidean Algorithm x = y1; y = x1 - y1 * (a / b); // store_gcd returns // the gcd of a and b return store_gcd;}// Function to find any// possible solutionpublic static int possible_solution(int a, int b, int c){ g = gcd(Math.Abs(a), Math.Abs(b)); // Condition if solution // does not exists if (c % g != 0) { return 0; } x0 *= c / g; y0 *= c / g; // Adjusting the sign // of x0 and y0 if (a < 0) x0 *= -1; if (b < 0) y0 *= -1; return 1;}// Function to shift solutionpublic static void shift_solution(int a, int b, int shift_var){ // Shifting to obtain // another solution x += shift_var * b; y -= shift_var * a;}// Function to find minimum// value of x and ypublic static int find_min_sum(int a, int b, int c){ int x = 0, y = 0, g = 0; // g is the gcd of a and b if (possible_solution(a, b, c) == 0) return -1; if (g != 0) { a /= g; b /= g; } // Store sign of a and b int sign_a = a > 0 ? +1 : -1; int sign_b = b > 0 ? +1 : -1; shift_solution(a, b, -x / b); // If x is less than 0, // then shift solution if (x < 0) shift_solution(a, b, sign_b); int minx1 = x; shift_solution(a, b, y / a); // If y is less than 0, // then shift solution if (y < 0) shift_solution(a, b, -sign_a); int minx2 = x; if (minx2 > x) { int temp = minx2; minx2 = x; x = temp; } int minx = Math.Max(minx1, minx2); // Find intersection such // that both x and y are positive if (minx > x) return -1; // miny is value of y // corresponding to minx int miny = (c - a * x) / b; // Returns minimum value // of x + y return (miny + minx);}// Driver Codepublic static void Main(String[] args){ // Given a, b, and c int a = 2, b = 2, c = 0; // Function call Console.Write(find_min_sum(a, b, c));}}// This code is contributed by Chitranayal |
0
Recommended Posts:
- 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
- Maximum possible sum of squares of stack elements satisfying the given properties
- 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
- Count possible permutations of given array satisfying the given conditions
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.


