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.
Input: A = 1, B = 2, S = 7, N = 5
3 * A + 2 * B = S
3 * 1 + 2 * 2 = 7
Input: A = 2, B = 1, S = 7, N = 5
Input: A = 2, B = 1, S = 4, N = 5
Input: A = 2, B = 3, S = 20, N = 8
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:
- Find minimum number of currency notes and values that sum to given amount
- 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 closing brackets needed to complete a regular bracket sequence
- Check if it is possible to serve customer queue with different notes
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count number of triplets with product equal to given number with duplicates allowed
- Find count of digits in a number that divide the number
- Count the number of operations required to reduce the given number
- Count number of ways to divide a number in 4 parts
- Count Number of animals in a zoo from given number of head and legs
- Count number of digits after decimal on dividing a number
- Count the number of occurrences of a particular digit in a number
- Count number of subsets of a set with GCD equal to a given number
- Minimum numbers needed to express every integer below N as a sum
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.