Last active
January 6, 2022 09:36
-
-
Save jatinsharrma/ae4f97e461d46a8582b5f75e7a94700a to your computer and use it in GitHub Desktop.
Coins problem: You have coins of 5 and 1 only. You have to find how much you need to make the given number. If you can't output -1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You have x no. of 5 rupee coins and y no. of 1 rupee coins. You want to purchase an item for amount z. | |
#The shopkeeper wants you to provide exact change. You want to pay using minimum number of coins. | |
#How many 5 rupee coins and 1 rupee coins will you use? If exact change is not possible then display -1. | |
def make_amount(rupees_to_make,no_of_five,no_of_one): | |
five_needed=0 | |
one_needed=0 | |
five = int(rupees_to_make/5) | |
one_needed = rupees_to_make%5 | |
five_needed = five if five <= no_of_five else no_of_five | |
if (five > no_of_five): | |
one_needed = rupees_to_make - 5 * no_of_five | |
(print("No. of Five needed : {}\nNo. of One needed : {}".format(five_needed,one_needed))) if one_needed <= no_of_one else (print(-1)) | |
make_amount(28,8,5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment