Data Structures & Algorithms Multiple Choice Questions on “Subset Sum Problem”.
1. Under what condition any set A will be a subset of B? Answer: b 2. What is a subset sum problem? Answer: b 3. Which of the following is true about the time complexity of the recursive solution of the subset sum problem? Answer: a 4. What is the worst case time complexity of dynamic programming solution of the subset sum problem(sum=given subset sum)? Answer: d 5. Subset sum problem is an example of NP-complete problem. Answer: a 6. Recursive solution of subset sum problem is faster than dynamic problem solution in terms of time complexity. Answer: b 7. Which of the following is not true about subset sum problem? 8. Which of the following should be the base case for the recursive solution of subset sum problem? b) c) d) 9. What will be the output for the following code? a) 12 Answer: c 10. What will be the output for the following code? a) true Answer: b 11. What will be the worst case time complexity for the following code? a) O(n log n) Answer: c complete set of 1000+
a) if all elements of set B are also present in set A
b) if all elements of set A are also present in set B
c) if A contains more elements than B
d) if B contains more elements than A
Clarification: Any set A will be called a subset of set B if all elements of set A are also present in set B. So in such a case set A will be a part of set B.
a) finding a subset of a set that has sum of elements equal to a given number
b) checking for the presence of a subset that has sum of elements equal to a given number and printing true or false based on the result
c) finding the sum of elements present in a set
d) finding the sum of all the subsets of a set
Clarification: In subset sum problem check for the presence of a subset that has sum of elements equal to a given number. If such a subset is present then we print true otherwise false.
a) It has an exponential time complexity
b) It has a linear time complexity
c) It has a logarithmic time complexity
d) it has a time complexity of O(n2)
Clarification: Subset sum problem has both recursive as well as dynamic programming solution. The recursive solution has an exponential time complexity as it will require to check for all subsets in worst case.
a) O(n)
b) O(sum)
c) O(n2)
d) O(sum*n)
Clarification: Subset sum problem has both recursive as well as dynamic programming solution. The dynamic programming solution has a time complexity of O(n*sum) as it as a nested loop with limits from 1 to n and 1 to sum respectively.
a) true
b) false
Clarification: Subset sum problem takes exponential time when we implement a recursive solution. Subset sum problem is known to be a part of NP complete problems.
a) true
b) false
View Answer
Clarification: The recursive solution to subset sum problem takes exponential time complexity whereas the dynamic programming solution takes polynomial time complexity. So dynamic programming solution is faster in terms of time complexity.
a) the recursive solution has a time complexity of O(2n)
b) there is no known solution that takes polynomial time
c) the recursive solution is slower than dynamic programming solution
d) the dynamic programming solution has a time complexity of O(n log n)
Answer: d
Clarification: Recursive solution of subset sum problem is slower than dynamic problem solution in terms of time complexity. Dynamic programming solution has a time complexity of O(n*sum).
a) if(sum==0)
return true;
if(sum==0)
return true;
if (n ==0 && sum!= 0)
return false;
if (n ==0 && sum!= 0)
return false;
if(sum
Answer: b
Clarification: The base case condition defines the point at which the program should stop recursion. In this case we need to make sure that, the sum does not become 0 and there should be elements left in our array for recursion to happen.
#include
b) 4 6 2
c) True
d) False
Clarification: The given code represents the recursive approach of solving the subset sum problem. The output for the code will be true if any subset is found to have sum equal to the desired sum, otherwise false will be printed.#include
b) false
c) 0
d) error in code
Clarification: The given code represents the dynamic programming approach of solving the subset sum problem. The output for the code will be true if any subset is found to have sum equal to the desired sum, otherwise false will be printed.#include
b) O(n2)
c) O(2n)
d) O(n2 log n)
View Answer
Clarification: The given code represents the recursive approach solution of the subset sum problem. It has an exponential time complexity as it will require to check for all subsets in the worst case. It is equal to O(2n).
