Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  What is the worst case time complexity of fol... Start Learning for Free
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 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)
Correct answer is option 'D'. Can you explain this answer?
Verified Answer
What is the worst case time complexity of following implementation of ...
Following is the recurrence for given implementation of subset sum problem T(n) = 2T(n-1) + C1 T(0) = C1 Where C1 and C2 are some machine specific constants. The solution of recurrence is O(2^n) We can see it with the help of recurrence tree method
If we sum the above tree level by level, we get the following series T(n) = C1 + 2C1 + 4C1 + 8C1 + ...
The above series is Geometrical progression and there will be n terms in it.
So T(n) = O(2^n)
View all questions of this test
Most Upvoted Answer
What is the worst case time complexity of following implementation of ...
The worst case time complexity of the given implementation is O(2^n), where n is the size of the set.

This is because the implementation uses a recursive approach to check all possible subsets of the set. In the worst case, the algorithm needs to check all possible combinations of elements in the set to find a subset with the given sum. Since each element can either be included or excluded in the subset, there are 2^n possible subsets.

Therefore, the worst case time complexity of the implementation is O(2^n).
Explore Courses for Computer Science Engineering (CSE) exam

Similar Computer Science Engineering (CSE) Doubts

Question Description
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)Correct answer is option 'D'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about 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)Correct answer is option 'D'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for 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)Correct answer is option 'D'. Can you explain this answer?.
Solutions for 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)Correct answer is option 'D'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of 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)Correct answer is option 'D'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of 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)Correct answer is option 'D'. Can you explain this answer?, a detailed solution for 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)Correct answer is option 'D'. Can you explain this answer? has been provided alongside types of 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)Correct answer is option 'D'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice 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)Correct answer is option 'D'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam
Signup to solve all Doubts
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev