Question

In: Computer Science

Greedy algorithm for the minimum coin change problem (the number of required coins is given) function...

Greedy algorithm for the minimum coin change problem (the number of required coins is given)

function parameters :

- int amount = 20

- int Coins[] = [1, 5, 11, 25]

- int requiredCoins = 4

Expected result = 5,5,5,5,

****algorithm should be greedy, and find optimal solution***

You can provide the program or the algorithm

Solutions

Expert Solution

In this set of given coins, following greedy algorithm will give optimal result:-

1. Given amount amount N, perform division by coin highest denomination I.e. 25 and store the value of N/25 as number of coins of denomination 25.

2. If N%25 ==0 then stop else set N = N%25 and repeat the task for value 11 and so on till the reminder is zero.

Below is the formal C code, in which starting from highest index n-1 of array Coins, we are dividing it by denomination and store the quotient as the number of coins of that denomination. Then reminder of the division we are storing. If reminder is zero then return result , else repeat the process with lower denomination coin.

int Number_of_Coins(int amount, int Coins[], int n)

{

int k; //used as iterator

int result = 0; //this will store total number of coins

for (k=n-1 ; k>=0 ; k--) //iteration from highest denomination

{

result = result + amount / Coins[k] ;

amount = amount % Coins[k]; //remaining amount left

if (amount==0) //then return the result

return (result);

}

}

Note that above program will work and given optimal result for the given Coins[] = [1, 5, 11, 25] and not always. Greedy algorithm will not work for all denominations. For general case, we have to use dynamic programming approach.


Related Solutions

Python Coding Question: Find the minimum number of coins that make a given value.
Python Coding Question: Find the minimum number of coins that make a given value.
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
java code for scheduling problem (start time,end time , profite) using greedy algorithm
java code for scheduling problem (start time,end time , profite) using greedy algorithm
Construct the visualization matrix for the following coin problem and find its optimal solution: Coins: Penny,...
Construct the visualization matrix for the following coin problem and find its optimal solution: Coins: Penny, Nickle, Dime, and Quarter The amount= 27cents USE DYNAMIC PROGRAMMING METHOD Dynamic Programming Let c[k,x] be the minimum number of coins for the amount x using the first k coins.          Goal: find a recurrence relation for c[k, x].        There are only two possible choices:            1. amount x includes the largest coin which is dk                  c[k, x] = 1 + c[k,...
Given 12 coins, with possibly (but not necessarily) one coin being counterfeit (heavy or light). You...
Given 12 coins, with possibly (but not necessarily) one coin being counterfeit (heavy or light). You have a comparison scale and you want to determine the fairness of all with a minimum number of comparisons. (a) What is the smallest number of comparisons required? Give a theoretical reason. (Hint: each comparison has three possible outcomes) (b) Give the procedure to identify the false coin or to show all are fair.
Given 12 coins, with possibly (but not necessarily) one coin being counterfeit (heavy or light). You...
Given 12 coins, with possibly (but not necessarily) one coin being counterfeit (heavy or light). You have a comparison scale and you want to determine the fairness of all with a minimum number of comparisons. (a) Give the procedure to identify the false coin or to show all are fair.
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
A biased coin has probability of p =0.52 for heads. What is the minimum number of...
A biased coin has probability of p =0.52 for heads. What is the minimum number of coin tosses needed such that there will be more heads than tails with 99% probability.
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT