InterviewSolution
Saved Bookmarks
| 1. |
What is the worst case time complexity of following implementation of subset sum problem.// Returns true if there is a subset of set[] with sun equal to given sumbool isSubsetSum(int set[], int n, int sum){// Base Casesif (sum == 0)return true;if (n == 0 && sum != 0)return false;// If last element is greater than sum, then ignore itif (set[n-1] > sum)return isSubsetSum(set, n-1, sum);/* else, check if sum can be obtained by any of the following(a) including the last element(b) excluding the last element */return isSubsetSum(set, n-1, sum) ||isSubsetSum(set, n-1, sum-set[n-1]);}(A) O(n * 2^n)(B) O(n^2)(C) O(n^2 * 2^n)(D) O(2^n) |
| Answer» None | |