# Data Structure Questions and Answers-Coin Change Problem

## Click on any option to know the CORRECT ANSWERS

 Question 1
You are given infinite coins of denominations v1, v2, v3, ....., vn and a sum S. The coin change problem is to find the minimum number of coins required to get the sum S. This problem can be solved using .....
 A Greedy algorithm B Dynamic programming C Divide and conquer D All of the mentioned

Question 1 Explanation:
The coin change problem has overlapping subproblems(same subproblems are solved multiple times) and optimal substructure(the solution to the problem can be found by finding optimal solutions for subproblems). So, dynamic programming can be used to solve the coin change problem.

 Question 2
Suppose you have coins of denominations 1, 3 and 4. You use a greedy algorithm, in which you choose the largest denomination coin which is not greater than the remaining sum. For which of the following sums, will the algorithm NOT produce an optimal answer?
 A 20 B 12 C 6 D 5

Question 2 Explanation:
Using the greedy algorithm, three coins {4, 1, 1} will be selected to make a sum of 6. But, the optimal answer is two coins {3, 3}.

 Question 3
Suppose you have coins of denominations 1, 3 and 4. You use a greedy algorithm, in which you choose the largest denomination coin which is not greater than the remaining sum. For which of the following sums, will the algorithm produce an optimal answer?
 A 14 B 10 C 6 D 100

Question 3 Explanation:
Using the greedy algorithm, three coins {4, 1, 1} will be selected to make a sum of 6. But, the optimal answer is two coins {3, 3}. Similarly, four coins {4, 4, 1, 1} will be selected to make a sum of 10. But, the optimal answer is three coins {4, 3, 3}. Also, five coins {4, 4, 4, 1, 1} will be selected to make a sum of 14. But, the optimal answer is four coins {4, 4, 3, 3}. For a sum of 100, twenty-five coins {all 4's} will be selected and the optimal answer is also twenty-five coins {all 4's}.

 Question 4
Fill in the blank to complete the code.

#include<stdio.h> int main() { int coins[10]={1, 3, 4}, lookup[100000]; int i, j, tmp, num....coins = 3, sum=100; lookup[0]=0; for(i = 1; i <= sum; i++) { 	 int min....coins = i; 	 for(j = 0;j < num....coins; j++) 	 { 	 tmp = i - coins[j]; 	 if(tmp < 0) 	 continue; 	 if(lookup[tmp] < min....coins) 	 .....; 	 } 	 lookup[i] = min....coins + 1; } printf("%d", lookup[sum]); return 0; }
 A lookup[tmp] = min....coins B min....coins = lookup[tmp] C break D continue

Question 4 Explanation:
min....coins = lookup[tmp] will complete the code.

 Question 5
You are given infinite coins of N denominations v1, v2, v3, ....., vn and a sum S. The coin change problem is to find the minimum number of coins required to get the sum S. What is the time complexity of a dynamic programming implementation used to solve the coin change problem?
 A O(N) B O(S) C O(N2) D O(S*N)