Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate the number of ways of representing n cents #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# README

<img src="https://s-media-cache-ak0.pinimg.com/564x/2d/8e/e8/2d8ee815146390d567706f2c7b5c2916.jpg" width="300">

This is a repo consisting of algorithms and data structures that I've implemented in Java.
Expand Down
44 changes: 44 additions & 0 deletions src/TechInterview/CoinsExchange.java
Original file line number Diff line number Diff line change
@@ -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));

}
}