Given two positive integers ‘a’ and ‘b’ that represent coefficients in equation ax + by = m. Find the minimum value of m that satisfies the equation for any positive integer values of x and y. And after this minimum value, the equation is satisfied by all (greater) values of m. If no such minimum value exists, return “-1”.

**Examples:**

Input: a = 4, b = 7 Output: 18 Explanation: 18 is the smallest value that can can be satisfied by equation 4x + 7y. 4*1 + 7*2 = 18 And after 18 all values are satisifed 4*3 + 7*1 = 19 4*5 + 7*0 = 20 ... and so on.

This is a variation of Frobenius coin problem. In Frobenius coin problem, we need to find the largest number that can not be represented using two coins. The largest amount for coins with denominations as ‘a’ and ‘b’ is a*b – (a+b). So the smallest number such that it can be represented using two coins and all numbers after it can also be represented is, a*b – (a+b) + 1.

One important case is when GCD of ‘a’ and ‘b’ is not 1. For example if ‘a’ = 4 and ‘b’ = 6, then all values that can be represented using two coins are even (or all values of m that can stratify the equation) are even. So all values that are NOT multiple of 2, cannot satisfy the equation. In this case there is no minimum value after which all values satisfy the equation.

Below is the implementation of above idea :

## C++

`// C++ program to find the minimum value of m that satisfies ` `// ax + by = m and all values after m also satisfy ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `findMin(` `int` `a, ` `int` `b) ` `{ ` ` ` `// If GCD is not 1, then there is no such value, ` ` ` `// else value is obtained using "a*b-a-b+1' ` ` ` `return` `(__gcd(a, b) == 1)? a*b-a-b+1 : -1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 4, b = 7; ` ` ` `cout << findMin(a, b) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the ` `// minimum value of m that ` `// satisfies ax + by = m ` `// and all values after m ` `// also satisfy ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `// Recursive function to ` `// return gcd of a and b ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Everything divides 0 ` ` ` `if` `(a == ` `0` `&& b == ` `0` `) ` ` ` `return` `0` `; ` ` ` ` ` `// base case ` ` ` `if` `(a == b) ` ` ` `return` `a; ` ` ` ` ` `// a is greater$ ` ` ` `if` `(a > b) ` ` ` `return` `__gcd(a - b, b); ` ` ` `return` `__gcd(a, b - a); ` `} ` ` ` `static` `int` `findMin( ` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// If GCD is not 1, then ` ` ` `// there is no such value, ` ` ` `// else value is obtained ` ` ` `// using "a*b-a-b+1' ` ` ` `return` `(__gcd(a, b) == ` `1` `)? ` ` ` `a * b - a - b + ` `1` `: -` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `a = ` `4` `; ` ` ` `int` `b = ` `7` `; ` ` ` `System.out.println(findMin(a, b)); ` `} ` `} ` ` ` `// This code is contributed ` `// by akt_mit ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the minimum ` `# value of m that satisfies ax + by = m ` `# and all values after m also satisfy ` ` ` `# Recursive function to return ` `# gcd of a and b ` `def` `__gcd(a, b): ` ` ` ` ` `# Everything divides 0 ` ` ` `if` `(a ` `=` `=` `0` `or` `b ` `=` `=` `0` `): ` ` ` `return` `0` `; ` ` ` ` ` `# base case ` ` ` `if` `(a ` `=` `=` `b): ` ` ` `return` `a; ` ` ` ` ` `# a is greater ` ` ` `if` `(a > b): ` ` ` `return` `__gcd(a ` `-` `b, b); ` ` ` `return` `__gcd(a, b ` `-` `a); ` ` ` `def` `findMin( a, b): ` ` ` ` ` `# If GCD is not 1, then ` ` ` `# there is no such value, ` ` ` `# else value is obtained ` ` ` `# using "a*b-a-b+1' ` ` ` `if` `(__gcd(a, b) ` `=` `=` `1` `): ` ` ` `return` `(a ` `*` `b ` `-` `a ` `-` `b ` `+` `1` `) ` ` ` `else` `: ` ` ` `return` `-` `1` ` ` `# Driver code ` `a ` `=` `4` `; ` `b ` `=` `7` `; ` `print` `(findMin(a, b)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the minimum ` `// value of m that satisfies ` `// ax + by = m and all values ` `// after m also satisfy ` `class` `GFG ` `{ ` `// Recursive function to ` `// return gcd of a and b ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Everything divides 0 ` ` ` `if` `(a == 0 && b == 0) ` ` ` `return` `0; ` ` ` ` ` `// base case ` ` ` `if` `(a == b) ` ` ` `return` `a; ` ` ` ` ` `// a is greater$ ` ` ` `if` `(a > b) ` ` ` `return` `__gcd(a - b, b); ` ` ` `return` `__gcd(a, b - a); ` `} ` ` ` `static` `int` `findMin( ` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// If GCD is not 1, then ` ` ` `// there is no such value, ` ` ` `// else value is obtained ` ` ` `// using "a*b-a-b+1' ` ` ` `return` `(__gcd(a, b) == 1)? ` ` ` `a * b - a - b + 1 : -1; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `a = 4; ` ` ` `int` `b = 7; ` ` ` `System.Console.WriteLine(findMin(a, b)); ` `} ` `} ` ` ` `// This code is contributed ` `// by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the ` `// minimum value of m that ` `// satisfies ax + by = m ` `// and all values after m ` `// also satisfy ` ` ` `// Recursive function to ` `// return gcd of a and b ` `function` `__gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` ` ` `// Everything divides 0 ` ` ` `if` `(` `$a` `== 0 ` `or` `$b` `== 0) ` ` ` `return` `0; ` ` ` ` ` `// base case ` ` ` `if` `(` `$a` `== ` `$b` `) ` ` ` `return` `$a` `; ` ` ` ` ` `// a is greater$ ` ` ` `if` `(` `$a` `> ` `$b` `) ` ` ` `return` `__gcd(` `$a` `- ` `$b` `, ` `$b` `); ` ` ` `return` `__gcd(` `$a` `, ` `$b` `- ` `$a` `); ` `} ` ` ` `function` `findMin( ` `$a` `, ` `$b` `) ` `{ ` ` ` ` ` `// If GCD is not 1, then ` ` ` `// there is no such value, ` ` ` `// else value is obtained ` ` ` `// using "a*b-a-b+1' ` ` ` `return` `(__gcd(` `$a` `, ` `$b` `) == 1)? ` ` ` `$a` `* ` `$b` `- ` `$a` `- ` `$b` `+ 1 : -1; ` `} ` ` ` ` ` `// Driver code ` ` ` `$a` `= 4; ` `$b` `= 7; ` ` ` `echo` `findMin(` `$a` `, ` `$b` `) ; ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

18

This article is contributed by **Rishabh Jain**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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:

- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Find count of numbers from 0 to n which satisfies the given equation for a value K
- Number of sextuplets (or six values) that satisfy an equation
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- All possible values of floor(N/K) for all values of K
- Find the lexicographically smallest string which satisfies the given condition
- Find permutation of first N natural numbers that satisfies the given condition
- Find an integer in the given range that satisfies the given conditions
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Count all prime numbers in a given range whose sum of digits is also prime
- Minimum increment/decrement operations required on Array to satisfy given conditions
- Find numbers a and b that satisfy the given conditions
- Find smallest perfect square number A such that N + A is also a perfect square number
- Count all possible N digit numbers that satisfy the given condition
- Maximum size of sub-array that satisfies the given condition
- Smallest index in the given array that satisfies the given condition
- Generate an array of size K which satisfies the given conditions
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Subarray permutation that satisfies the given condition
- Partition the digits of an integer such that it satisfies a given condition