Bird
Raised Fist0

What is the time complexity of the space-optimized bottom-up subset sum algorithm given an input list of size n and target sum S?

medium🪤 Complexity Trap Q13 of Q15
Dynamic Programming: Knapsack - Subset Sum
What is the time complexity of the space-optimized bottom-up subset sum algorithm given an input list of size n and target sum S?
AO(2^n)
BO(n + S)
CO(n * S)
DO(n * log S)
Step-by-Step Solution
Solution:
  1. Step 1: Identify loops in the algorithm

    The algorithm has an outer loop over n elements and an inner loop over sums from S down to each num.
  2. Step 2: Calculate total operations

    Each element causes up to S iterations, so total time is O(n * S). Recursive brute force is exponential, and linear or log factors are incorrect.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Nested loops over n and S -> O(n * S) [OK]
Quick Trick: Nested loops over n and S -> multiply complexities [OK]
Common Mistakes:
MISTAKES
  • Confusing recursion stack space with time
  • Assuming linear or exponential time incorrectly
Trap Explanation:
PITFALL
  • Some think time is O(n + S) because of initialization or confuse with brute force exponential time.
Interviewer Note:
CONTEXT
  • Checks understanding of DP complexity vs brute force and space optimization.
Master "Subset Sum" in Dynamic Programming: Knapsack

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Dynamic Programming: Knapsack Quizzes