From ccb2236899b5b8da1d215f47a5fbb9561225c900 Mon Sep 17 00:00:00 2001 From: Bogdan Popov Date: Thu, 20 Oct 2016 14:17:04 +0300 Subject: [PATCH] Coins exchange solution added --- README.md | 1 + src/TechInterview/CoinsExchange.java | 44 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/TechInterview/CoinsExchange.java diff --git a/README.md b/README.md index 190896d..522fe00 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # README + This is a repo consisting of algorithms and data structures that I've implemented in Java. diff --git a/src/TechInterview/CoinsExchange.java b/src/TechInterview/CoinsExchange.java new file mode 100644 index 0000000..445031f --- /dev/null +++ b/src/TechInterview/CoinsExchange.java @@ -0,0 +1,44 @@ +package TechInterview; + +public class CoinsExchange { + + /** + * Given an in infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), + * write code to calculate the number of ways of representing n cents. + */ + + static int changeCoins(int n) { + return changeCoinsWithCount(n, 4); + } + + static int changeCoinsWithCount(int amount, int countOfcoins) { + + if (amount == 0) + return 1; + if (amount < 0 || countOfcoins == 0) + return 0; + else + return changeCoinsWithCount(amount, countOfcoins - 1) + changeCoinsWithCount(amount - firstDenomination(countOfcoins), countOfcoins); + } + + static int firstDenomination(int countOfCoins) { + switch (countOfCoins) { + case 1: + return 1; + case 2: + return 5; + case 3: + return 10; + case 4: + return 25; + default: + return 0; + } + } + + public static void main(String[] args) { + + System.out.println(changeCoins(100)); + + } +} \ No newline at end of file