Frobenius coin problem - GeeksforGeeks

07 Jul, 2021
Given two coins of denominations “X” and “Y” respectively, find the largest amount that cannot be obtained using these two coins (assuming infinite supply of coins) followed by the total number of such non obtainable amounts, if no such value exists print “NA”.
Examples :
Input : X=2, Y=5  
Output: Largest amount = 3
        Total count  = 2
We cannot represent 1 and 3 from infinite supply
of given two coins. The largest among these 2 is 3.
We can represent all other amounts for example 13
can be represented 2*4 + 5.

Input : X=5, Y=10
Output: NA
There are infinite number of amounts that cannot
be represented by these two coins.

One important observation is, if GCD of X and Y is not one, then all values that can be formed by given two coins are multiples of GCD. For example if X = 4 and Y = 6. Then all values are multiple of 2. So all values that are not multiple of 2, cannot be formed by X and Y. Thus there exist infinitely many values that cannot be formed by 4 and 6, and our answer becomes “NA”.
This general problem for n coins is known as classic Forbenius coin problem.
When the number of coins is two, there is 
explicit formula if GCD is not 1. The formula
is:
  Largest amount A = (X * Y) - (X + Y)
  Total amount = (X -1) * (Y - 1) /2 
 

Hence, we can now easily answer the above question by following the below steps:
Calculate GCD of X and YIf GCD is 1 then required largest amount is (X*Y)-(X+Y) and total count is (X-1)*(Y-1)/2Else print “NA”Below is a the program based on the same.C++JavaPython3C#PHPC++// C++ program to find the largest number that// cannot be formed from given two coins#include <bits/stdc++.h>using namespace std;  // Utility function to find gcdint gcd(int a, int b){    int c;    while (a != 0)    {        c = a;        a = b%a;        b = c;    }    return b;}  // Function to print the desired outputvoid forbenius(int X,int Y){    // Solution doesn't exist     // if GCD is not 1    if (gcd(X,Y) != 1)    {        cout << "NA\n";        return;    }      // Else apply the formula    int A = (X*Y)-(X+Y);    int N = (X-1)*(Y-1)/2;      cout << "Largest Amount = " << A << endl;    cout << "Total Count = " << N << endl;}  // Driver Codeint main(){    int X = 2,Y = 5;    forbenius(X,Y);      X = 5, Y = 10;    cout << endl;    forbenius(X,Y);    return 0;}Java// Java program to find the largest// number that cannot be formed // from given two coinsimport java.io.*;  class GFG{// Utility function to find gcd    static int gcd(int a, int b)    {        int c;        while (a != 0)        {            c = a;            a = b % a;            b = c;        }        return b;    }      // Function to print the    // desired output    static void forbenius(int X,                           int Y)    {        // Solution doesn't exist         // if GCD is not 1        if (gcd(X, Y) != 1)        {            System.out.println( "NA");            return;        }              // Else apply the formula        int A = (X * Y) - (X + Y);        int N = (X - 1) * (Y - 1) / 2;              System.out.println("Largest Amount = " + A );        System.out.println("Total Count = " + N );    }          // Driver Code    public static void main(String[] args)    {        int X = 2,Y = 5;        forbenius(X, Y);        X = 5;        Y = 10;        System.out.println();        forbenius(X, Y);              }}  // This code is contributed by Sam007Python3# Python3 program to find the largest # number that cannot be formed# from given two coins  # Utility function to find gcddef gcd(a, b):    while (a != 0):        c = a;        a = b % a;        b = c;          return b;  # Function to print the desired outputdef forbenius(X, Y):      # Solution doesn't exist     # if GCD is not 1    if (gcd(X, Y) != 1):        print("NA");        return;      # Else apply the formula    A = (X * Y) - (X + Y);    N = (X - 1) * (Y - 1) // 2;      print("Largest Amount =", A);    print("Total Count =", N);  # Driver CodeX = 2; Y = 5;forbenius(X, Y);  X = 5; Y = 10;print("");forbenius(X, Y);  # This code is contributed by mitsC#// C# program to find the largest//  number that cannot be formed // from given two coinsusing System;  class GFG{// Utility function to find gcd    static int gcd(int a, int b)    {        int c;        while (a != 0)        {            c = a;            a = b%a;            b = c;        }        return b;    }      // Function to print the    // desired output    static void forbenius(int X, int Y)    {        // Solution doesn't exist         // if GCD is not 1        if (gcd(X,Y) != 1)        {            Console.WriteLine( "NA");            return;        }              // Else apply the formula        int A = (X * Y) - (X + Y);        int N = (X - 1) * (Y - 1) / 2;              Console.WriteLine("Largest Amount = " + A );        Console.WriteLine("Total Count = " + N );    }                        // Driver Code    public static void Main()    {        int X = 2,Y = 5;        forbenius(X,Y);        X = 5;        Y = 10;        Console.WriteLine();        forbenius(X,Y);          }}      // This code is contributed by Sam007PHP<?php// php program to find the largest // number that cannot be formed// from given two coins  // Utility function to find gcdfunction gcd($a, $b){    $c;    while ($a != 0)    {        $c = $a;        $a = $b % $a;        $b = $c;    }          return $b;}  // Function to print the desired outputfunction forbenius($X, $Y){    // Solution doesn't exist     // if GCD is not 1    if (gcd($X, $Y) != 1)    {        echo "NA\n";        return;    }      // Else apply the formula    $A = ($X * $Y) - ($X + $Y);    $N = ($X - 1) * ($Y - 1) / 2;      echo "Largest Amount = ", $A, "\n";    echo "Total Count = ", $N, "\n";}  // Driver Code      $X = 2; $Y = 5;    forbenius($X, $Y);      $X = 5; $Y = 10;    echo "\n";    forbenius($X, $Y);  // This code is contributed by ajit.?>Output :Largest Amount = 3
Total Count = 2

NA
References:https://en.wikipedia.org/wiki/Coin_problemThis article is contributed by Ashutosh Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed aboveMy Personal Notes
arrow_drop_upSave
Calculate GCD of X and Y
If GCD is 1 then required largest amount is (X*Y)-(X+Y) and total count is (X-1)*(Y-1)/2
Else print “NA”Below is a the program based on the same.C++JavaPython3C#PHPC++// C++ program to find the largest number that// cannot be formed from given two coins#include <bits/stdc++.h>using namespace std;  // Utility function to find gcdint gcd(int a, int b){    int c;    while (a != 0)    {        c = a;        a = b%a;        b = c;    }    return b;}  // Function to print the desired outputvoid forbenius(int X,int Y){    // Solution doesn't exist     // if GCD is not 1    if (gcd(X,Y) != 1)    {        cout << "NA\n";        return;    }      // Else apply the formula    int A = (X*Y)-(X+Y);    int N = (X-1)*(Y-1)/2;      cout << "Largest Amount = " << A << endl;    cout << "Total Count = " << N << endl;}  // Driver Codeint main(){    int X = 2,Y = 5;    forbenius(X,Y);      X = 5, Y = 10;    cout << endl;    forbenius(X,Y);    return 0;}Java// Java program to find the largest// number that cannot be formed // from given two coinsimport java.io.*;  class GFG{// Utility function to find gcd    static int gcd(int a, int b)    {        int c;        while (a != 0)        {            c = a;            a = b % a;            b = c;        }        return b;    }      // Function to print the    // desired output    static void forbenius(int X,                           int Y)    {        // Solution doesn't exist         // if GCD is not 1        if (gcd(X, Y) != 1)        {            System.out.println( "NA");            return;        }              // Else apply the formula        int A = (X * Y) - (X + Y);        int N = (X - 1) * (Y - 1) / 2;              System.out.println("Largest Amount = " + A );        System.out.println("Total Count = " + N );    }          // Driver Code    public static void main(String[] args)    {        int X = 2,Y = 5;        forbenius(X, Y);        X = 5;        Y = 10;        System.out.println();        forbenius(X, Y);              }}  // This code is contributed by Sam007Python3# Python3 program to find the largest # number that cannot be formed# from given two coins  # Utility function to find gcddef gcd(a, b):    while (a != 0):        c = a;        a = b % a;        b = c;          return b;  # Function to print the desired outputdef forbenius(X, Y):      # Solution doesn't exist     # if GCD is not 1    if (gcd(X, Y) != 1):        print("NA");        return;      # Else apply the formula    A = (X * Y) - (X + Y);    N = (X - 1) * (Y - 1) // 2;      print("Largest Amount =", A);    print("Total Count =", N);  # Driver CodeX = 2; Y = 5;forbenius(X, Y);  X = 5; Y = 10;print("");forbenius(X, Y);  # This code is contributed by mitsC#// C# program to find the largest//  number that cannot be formed // from given two coinsusing System;  class GFG{// Utility function to find gcd    static int gcd(int a, int b)    {        int c;        while (a != 0)        {            c = a;            a = b%a;            b = c;        }        return b;    }      // Function to print the    // desired output    static void forbenius(int X, int Y)    {        // Solution doesn't exist         // if GCD is not 1        if (gcd(X,Y) != 1)        {            Console.WriteLine( "NA");            return;        }              // Else apply the formula        int A = (X * Y) - (X + Y);        int N = (X - 1) * (Y - 1) / 2;              Console.WriteLine("Largest Amount = " + A );        Console.WriteLine("Total Count = " + N );    }                        // Driver Code    public static void Main()    {        int X = 2,Y = 5;        forbenius(X,Y);        X = 5;        Y = 10;        Console.WriteLine();        forbenius(X,Y);          }}      // This code is contributed by Sam007PHP<?php// php program to find the largest // number that cannot be formed// from given two coins  // Utility function to find gcdfunction gcd($a, $b){    $c;    while ($a != 0)    {        $c = $a;        $a = $b % $a;        $b = $c;    }          return $b;}  // Function to print the desired outputfunction forbenius($X, $Y){    // Solution doesn't exist     // if GCD is not 1    if (gcd($X, $Y) != 1)    {        echo "NA\n";        return;    }      // Else apply the formula    $A = ($X * $Y) - ($X + $Y);    $N = ($X - 1) * ($Y - 1) / 2;      echo "Largest Amount = ", $A, "\n";    echo "Total Count = ", $N, "\n";}  // Driver Code      $X = 2; $Y = 5;    forbenius($X, $Y);      $X = 5; $Y = 10;    echo "\n";    forbenius($X, $Y);  // This code is contributed by ajit.?>Output :Largest Amount = 3
Total Count = 2

NA
References:https://en.wikipedia.org/wiki/Coin_problemThis article is contributed by Ashutosh Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed aboveMy Personal Notes
arrow_drop_upSave
Below is a the program based on the same.
C++
Java
Python3
C#
PHP
// C++ program to find the largest number that// cannot be formed from given two coins#include <bits/stdc++.h>using namespace std;  // Utility function to find gcdint gcd(int a, int b){    int c;    while (a != 0)    {        c = a;        a = b%a;        b = c;    }    return b;}  // Function to print the desired outputvoid forbenius(int X,int Y){    // Solution doesn't exist     // if GCD is not 1    if (gcd(X,Y) != 1)    {        cout << "NA\n";        return;    }      // Else apply the formula    int A = (X*Y)-(X+Y);    int N = (X-1)*(Y-1)/2;      cout << "Largest Amount = " << A << endl;    cout << "Total Count = " << N << endl;}  // Driver Codeint main(){    int X = 2,Y = 5;    forbenius(X,Y);      X = 5, Y = 10;    cout << endl;    forbenius(X,Y);    return 0;}
// Java program to find the largest// number that cannot be formed // from given two coinsimport java.io.*;  class GFG{// Utility function to find gcd    static int gcd(int a, int b)    {        int c;        while (a != 0)        {            c = a;            a = b % a;            b = c;        }        return b;    }      // Function to print the    // desired output    static void forbenius(int X,                           int Y)    {        // Solution doesn't exist         // if GCD is not 1        if (gcd(X, Y) != 1)        {            System.out.println( "NA");            return;        }              // Else apply the formula        int A = (X * Y) - (X + Y);        int N = (X - 1) * (Y - 1) / 2;              System.out.println("Largest Amount = " + A );        System.out.println("Total Count = " + N );    }          // Driver Code    public static void main(String[] args)    {        int X = 2,Y = 5;        forbenius(X, Y);        X = 5;        Y = 10;        System.out.println();        forbenius(X, Y);              }}  // This code is contributed by Sam007
# Python3 program to find the largest # number that cannot be formed# from given two coins  # Utility function to find gcddef gcd(a, b):    while (a != 0):        c = a;        a = b % a;        b = c;          return b;  # Function to print the desired outputdef forbenius(X, Y):      # Solution doesn't exist     # if GCD is not 1    if (gcd(X, Y) != 1):        print("NA");        return;      # Else apply the formula    A = (X * Y) - (X + Y);    N = (X - 1) * (Y - 1) // 2;      print("Largest Amount =", A);    print("Total Count =", N);  # Driver CodeX = 2; Y = 5;forbenius(X, Y);  X = 5; Y = 10;print("");forbenius(X, Y);  # This code is contributed by mits
// C# program to find the largest//  number that cannot be formed // from given two coinsusing System;  class GFG{// Utility function to find gcd    static int gcd(int a, int b)    {        int c;        while (a != 0)        {            c = a;            a = b%a;            b = c;        }        return b;    }      // Function to print the    // desired output    static void forbenius(int X, int Y)    {        // Solution doesn't exist         // if GCD is not 1        if (gcd(X,Y) != 1)        {            Console.WriteLine( "NA");            return;        }              // Else apply the formula        int A = (X * Y) - (X + Y);        int N = (X - 1) * (Y - 1) / 2;              Console.WriteLine("Largest Amount = " + A );        Console.WriteLine("Total Count = " + N );    }                        // Driver Code    public static void Main()    {        int X = 2,Y = 5;        forbenius(X,Y);        X = 5;        Y = 10;        Console.WriteLine();        forbenius(X,Y);          }}      // This code is contributed by Sam007
<?php// php program to find the largest // number that cannot be formed// from given two coins  // Utility function to find gcdfunction gcd($a, $b){    $c;    while ($a != 0)    {        $c = $a;        $a = $b % $a;        $b = $c;    }          return $b;}  // Function to print the desired outputfunction forbenius($X, $Y){    // Solution doesn't exist     // if GCD is not 1    if (gcd($X, $Y) != 1)    {        echo "NA\n";        return;    }      // Else apply the formula    $A = ($X * $Y) - ($X + $Y);    $N = ($X - 1) * ($Y - 1) / 2;      echo "Largest Amount = ", $A, "\n";    echo "Total Count = ", $N, "\n";}  // Driver Code      $X = 2; $Y = 5;    forbenius($X, $Y);      $X = 5; $Y = 10;    echo "\n";    forbenius($X, $Y);  // This code is contributed by ajit.?>
Largest Amount = 3
Total Count = 2

NA

References:https://en.wikipedia.org/wiki/Coin_problem
This article is contributed by Ashutosh Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Sam007
jit_t
Mithun Kumar
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
Program for factorial of a number
The Knight's tour problem | Backtracking-1
Program to find sum of elements in a given array
Euclidean algorithms (Basic and Extended)
Program to print prime numbers from 1 to N.
Minimum number of jumps to reach end