# Data Structure Questions and Answers – Coin Change Problem

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Coin Change Problem”.

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) Backtracking

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.

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

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}.

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

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}.

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

Explanation: min_coins = lookup[tmp] will complete the code.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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)

Explanation: The time complexity is O(S*N).

6. Suppose 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 space 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)

Explanation: To get the optimal solution for a sum S, the optimal solution is found for each sum less than equal to S and each solution is stored. So, the space complexity is O(S).

7. You are given infinite coins of denominations 1, 3, 4. What is the total number of ways in which a sum of 7 can be achieved using these coins if the order of the coins is not important?
a) 4
b) 3
c) 5
d) 6

Explanation: A sum of 7 can be achieved in the following ways:
{1,1,1,1,1,1,1}, {1,1,1,1,3}, {1,3,3}, {1,1,1,4}, {3,4}.
Therefore, the sum can be achieved in 5 ways.

8. You are given infinite coins of denominations 1, 3, 4. What is the minimum number of coins required to achieve a sum of 7?
a) 1
b) 2
c) 3
d) 4

Explanation: A sum of 7 can be achieved by using a minimum of two coins {3,4}.

9. You are given infinite coins of denominations 5, 7, 9. Which of the following sum CANNOT be achieved using these coins?
a) 50
b) 21
c) 13
d) 23

Explanation: One way to achieve a sum of 50 is to use ten coins of 5. A sum of 21 can be achieved by using three coins of 7. One way to achieve a sum of 23 is to use two coins of 7 and one coin of 9. A sum of 13 cannot be achieved.

10. You are given infinite coins of denominations 3, 5, 7. Which of the following sum CANNOT be achieved using these coins?
a) 15
b) 16
c) 17
d) 4

Explanation: Sums can be achieved as follows:
15 = {5,5,5}
16 = {3,3,5,5}
17 = {3,7,7}
we can’t achieve for sum=4 because our available denominations are 3,5,7 and sum of any two denominations is greater than 4.

11. What is the output of the following program?

```#include<stdio.h>
int main()
{
int coins[10]={1,3,4},lookup[100];
int i,j,tmp,num_coins = 3,sum=10;
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)
min_coins=lookup[tmp];
}
lookup[i] = min_coins + 1;
}
printf("%d",lookup[sum]);
return 0;
}```

a) 2
b) 3
c) 4
d) 5

Explanation: The program prints the minimum number of coins required to get a sum of 10, which is 3.

12. What is the output of the following program?

```#include<stdio.h>
int main()
{
int coins[10]={1,3,4},lookup[100];
int i,j,tmp,num_coins = 3,sum=14;
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)
min_coins=lookup[tmp];
}
lookup[i] = min_coins + 1;
}
printf("%d",lookup[sum]);
return 0;
}```

a) 2
b) 3
c) 4
d) 5

Explanation: The program prints the minimum number of coins required to get a sum of 14, which is 4.

Sanfoundry Global Education & Learning Series – Data Structures & Algorithms.

To practice all areas of Data Structures & Algorithms, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]