Count the number of currency notes needed - GeeksforGeeks

04 May, 2021
You have an unlimited amount of banknotes worth A and B dollars (A not equals to B). You want to pay a total of S dollars using exactly N notes. The task is to find the number of notes worth A dollars you need. If there is no solution return -1.
Examples: 
Input: A = 1, B = 2, S = 7, N = 5 Output: 3 3 * A + 2 * B = S 3 * 1 + 2 * 2 = 7
Input: A = 2, B = 1, S = 7, N = 5 Output: 2
Input: A = 2, B = 1, S = 4, N = 5 Output: -1
Input: A = 2, B = 3, S = 20, N = 8 Output: 4 
Approach: Let x be the number of notes of value A required then the rest of the notes i.e. N – x must of value B. Since, their sum is required be S then the following equation must be satisfied:  
S = (A * x) + (B * (N – x)) Solving the equation further, S = (A * x) + (B * N) – (B * x) S – (B * N) = (A – B) * x x = (S – (B * N)) / (A – B) After solving for x, it is the number of notes of value A required only if the value of x is an integer else the result is not possible.  
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include<bits/stdc++.h>using namespace std; // Function to return the amount of notes// with value A requiredint bankNotes(int A, int B, int S, int N){    int numerator = S - (B * N);    int denominator = A - B;     // If possible    if (numerator % denominator == 0)        return (numerator / denominator);    return -1;} // Driver codeint main(){    int A = 1, B = 2, S = 7, N = 5;    cout << bankNotes(A, B, S, N) << endl;}     // This code is contributed by mits
// Java implementation of the approachclass GFG {     // Function to return the amount of notes    // with value A required    static int bankNotes(int A, int B, int S, int N)    {        int numerator = S - (B * N);        int denominator = A - B;         // If possible        if (numerator % denominator == 0)            return (numerator / denominator);        return -1;    }     // Driver code    public static void main(String[] args)    {        int A = 1, B = 2, S = 7, N = 5;        System.out.print(bankNotes(A, B, S, N));    }}
# Python3 implementation of the approach # Function to return the amount of notes# with value A requireddef bankNotes(A, B, S, N):     numerator = S - (B * N)    denominator = A - B     # If possible    if (numerator % denominator == 0):        return (numerator // denominator)    return -1 # Driver codeA, B, S, N = 1, 2, 7, 5print(bankNotes(A, B, S, N)) # This code is contributed# by mohit kumar
// C# implementation of the approachusing System; class GFG{     // Function to return the amount of notes    // with value A required    static int bankNotes(int A, int B,                         int S, int N)    {        int numerator = S - (B * N);        int denominator = A - B;         // If possible        if (numerator % denominator == 0)            return (numerator / denominator);        return -1;    }     // Driver code    public static void Main()    {        int A = 1, B = 2, S = 7, N = 5;        Console.Write(bankNotes(A, B, S, N));    }} // This code is contributed by Akanksha Rai
<?php// PHP implementation of the approach // Function to return the amount of notes// with value A requiredfunction bankNotes($A, $B, $S, $N){    $numerator = $S - ($B * $N);    $denominator = $A - $B;     // If possible    if ($numerator % $denominator == 0)        return ($numerator / $denominator);    return -1;} // Driver code$A = 1; $B= 2; $S = 7; $N = 5;echo(bankNotes($A, $B, $S, $N)); // This code is contributed by Code_Mech?>
<script> // Javascript implementation of the approach // Function to return the amount of notes// with value A requiredfunction bankNotes(A, B, S, N){    let numerator = S - (B * N);    let denominator = A - B;     // If possible    if (numerator % denominator == 0)        return (Math.floor(numerator /                           denominator));    return -1;} // Driver Codelet A = 1, B = 2, S = 7, N = 5; document.write(bankNotes(A, B, S, N) + "</br>"); // This code is contributed by jana_sayantan </script>
3
 
mohit kumar 29
Akanksha_Rai
Code_Mech
Mithun Kumar
jana_sayantan
expression-evaluation
Greedy
Mathematical
School Programming
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Optimal Page Replacement Algorithm
Program for Best Fit algorithm in Memory Management
Program for First Fit algorithm in Memory Management
Bin Packing Problem (Minimize number of used Bins)
Program for Worst Fit algorithm in Memory Management
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples