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”.
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 :
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 firstname.lastname@example.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.
- Number of sextuplets (or six values) that satisfy an equation
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Find an integer in the given range that satisfies the given conditions
- Find permutation of first N natural numbers that satisfies the given condition
- Find the lexicographically smallest string which satisfies the given condition
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z
- Find numbers a and b that satisfy the given conditions
- Find n positive integers that satisfy the given equations
- Maximum and Minimum Values of an Algebraic Expression
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Minimize the difference between the maximum and minimum values of the modified array
- Find smallest values of x and y such that ax - by = 0
- Find the values of X and Y in the Given Equations
- Find the sum of the ascii values of characters which are present at prime positions
- Subarray permutation that satisfies the given condition