Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
Last Updated :
07 Jul, 2022
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 be satisfied by equation 4x + 7y.
4*1 + 7*2 = 18
And after 18 all values are satisfied
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++
#include<bits/stdc++.h>
using namespace std;
int findMin( int a, int b)
{
return (__gcd(a, b) == 1)? a*b-a-b+1 : -1;
}
int main()
{
int a = 4, b = 7;
cout << findMin(a, b) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0 && b == 0 )
return 0 ;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int findMin( int a, int b)
{
return (__gcd(a, b) == 1 )?
a * b - a - b + 1 : - 1 ;
}
public static void main (String[] args)
{
int a = 4 ;
int b = 7 ;
System.out.println(findMin(a, b));
}
}
|
Python3
def __gcd(a, b):
if (a = = 0 or b = = 0 ):
return 0 ;
if (a = = b):
return a;
if (a > b):
return __gcd(a - b, b);
return __gcd(a, b - a);
def findMin( a, b):
if (__gcd(a, b) = = 1 ):
return (a * b - a - b + 1 )
else :
return - 1
a = 4 ;
b = 7 ;
print (findMin(a, b));
|
C#
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0 && b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int findMin( int a, int b)
{
return (__gcd(a, b) == 1)?
a * b - a - b + 1 : -1;
}
public static void Main()
{
int a = 4;
int b = 7;
System.Console.WriteLine(findMin(a, b));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0 or $b == 0)
return 0;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function findMin( $a , $b )
{
return (__gcd( $a , $b ) == 1)?
$a * $b - $a - $b + 1 : -1;
}
$a = 4; $b = 7;
echo findMin( $a , $b ) ;
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0 && b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function findMin(a, b)
{
return (__gcd(a, b) == 1)?
a * b - a - b + 1 : -1;
}
let a = 4;
let b = 7;
document.write(findMin(a, b));
</script>
|
Output:
18
Time Complexity: O(log(min(a, b)), where a and b are the two given integers.
Auxiliary Space: O(1), no extra space required so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...