subset_sum_backtracking
subset_sum_backtracking
Problem Statement:
-------------------
Given a set of integers and a target sum, find whether there exists a subset whose
sum is equal to the target sum.
Algorithm (Pseudocode):
------------------------
Function SubsetSum(arr, n, target_sum):
Call backtrack(0, 0)
return False
Example:
---------
Input:
- Array = [3, 34, 4, 12, 5, 2]
- Target Sum = 9
Execution Steps:
- Start from index = 0, current_sum = 0
- Include 3 → current_sum = 3
- Exclude 34
- Include 4 → current_sum = 7
- Exclude 12
- Exclude 5
- Include 2 → current_sum = 9 → FOUND
Subset: [3, 4, 2]
Summary:
---------
Aspect | Value
--------------------|----------------------
Time Complexity | O(2^n)
Space Complexity | O(n)
Type | Exponential, Backtracking
Important | Prune when sum > target