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 forx, it is the number of notes of valueArequired

only if the value ofxis an integer else the result is not possible.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the amount of notes ` `// with value A required ` `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 ` `int` `main() ` `{ ` ` ` `int` `A = 1, B = 2, S = 7, N = 5; ` ` ` `cout << bankNotes(A, B, S, N) << endl; ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `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(String[] args) ` ` ` `{ ` ` ` `int` `A = ` `1` `, B = ` `2` `, S = ` `7` `, N = ` `5` `; ` ` ` `System.out.print(bankNotes(A, B, S, N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the amount of notes ` `# with value A required ` `def` `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, B, S, N ` `=` `1` `, ` `2` `, ` `7` `, ` `5` `print` `(bankNotes(A, B, S, N)) ` ` ` `# This code is contributed ` `# by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the amount of notes ` `// with value A required ` `function` `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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

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:

- Find minimum number of currency notes and values that sum to given amount
- Number of closing brackets needed to complete a regular bracket sequence
- Find minimum number of Log value needed to calculate Log upto N
- Minimum number of letters needed to make a total of n
- Number of cards needed build a House of Cards of a given level N
- Check if it is possible to serve customer queue with different notes
- Minimum steps needed to cover a sequence of points on an infinite grid
- Minimum numbers needed to express every integer below N as a sum
- Find minimum operations needed to make an Array beautiful
- Minimum Circles needed to be removed so that all remaining circles are non intersecting
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Minimum minutes needed to make the time palindromic
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count number of subsets of a set with GCD equal to a given number
- Count number of ways to divide a number in 4 parts
- Count number of digits after decimal on dividing a number
- Count number of triplets with product equal to given number with duplicates allowed
- Count Number of animals in a zoo from given number of head and legs
- Count number of trailing zeros in Binary representation of a number using Bitset

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.