Given A coins of value N and B coins of value M, the task is to check if given coins can be used to pay a value of S.
Input: A = 1, B = 2, N = 3, S = 4, M = 1
In this case if 1 coin of value 3 is chosen and 2 coins of value 1, then it is possible to pay a value of S.
Input: A = 1, B = 2, N = 3, S = 6, M = 1
In this case, It is not possible to pay a value of S
The idea is to use greedy approach.
- Keep subtracting coins with value N from the required sum S.
- At each step, while subtracting coins of value N, check if the remaining sum is a multiple of coins with value M and we have sufficient coins of value M to get this remaining sum.
- If at any step, the above two conditions are satisfied, return YES.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum cost for acquiring all coins with k extra coins allowed with every coin
- Buy minimum items without change and given coins
- Probability of getting at least K heads in N tosses of Coins
- Find minimum number of coins that make a given value
- Maximum items that can be bought with the given type of coins
- Greedy Algorithm to find Minimum number of Coins
- Program to find the count of coins of each type from the given ratio
- Find out the minimum number of coins required to pay total amount
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Check whether it is possible to convert A into B
- Check whether N is a Factorion or not
- Check whether the exchange is possible or not
- Check n^2 - m^2 is prime or not
- Check whether a given number is even or odd
- Check divisibility by 7
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.