DAA Expt-6
DAA Expt-6
Experiment-6
Student Name: AMIT KUMAR UID: 22BCS10936
Branch: BE-CSE Section/Group: 22BCS_IOT-639-A
Semester: 5th Date of Performance: 11.09.2024
Subject Name: DAA Subject Code: 22CSH-311
1. Aim
Develop a program and analyze complexity to implement subset-sum problem using
Dynamic Programming.
2. Objective
• The main objective is to find if there is a subset of the given set of integers that adds up
to a target sum.
• Implement the solution using Dynamic Programming to reduce the time complexity
compared to a brute-force approach (which would take exponential time).
3. Code
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
}
}
if (!dp[n][target]) {
return null;
}
return subset;
}
if (subset != null) {
System.out.println("A subset with the target sum exists: " + subset);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
} else {
System.out.println("No subset with the target sum exists.");
}
}
}
4. Output
amit
5. Time Complexity
Time complexity is: O(n * target)
6. Learning Outcomes
• Learn how to break a problem into smaller subproblems.
• Learn to analyze the time and space complexity of dynamin programming.
• Understand how to trace back from the DP table to extract the actual subset that
contributes to the target sum.