• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Algorithms | Analysis of Algorithms (Recurrences) | Question 3

What is the worst case time complexity of following implementation of subset sum problem. 

C
// Returns true if there is a subset of set[] with sum equal to given sum
bool isSubsetSum(int set[], int n, int sum)
{
   // Base Cases
   if (sum == 0)
     return true;
   if (n == 0 && sum != 0)
     return false;
 
   // If last element is greater than sum, then ignore it
   if (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

Please comment below if you find anything wrong in the above post
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated :
Share your thoughts in the comments