Algo-Phantoms / Algo-Tree

Algo-Tree is a collection of Algorithms and data structures which are fundamentals to efficient code and good software design. Creating and designing excellent algorithms is required for being an exemplary programmer. It contains solutions in various languages such as C++, Python and Java.
MIT License
357 stars 624 forks source link

Subset sum problem using DP #2004

Closed Fatema110 closed 3 years ago

Fatema110 commented 3 years ago

Solution Here we are taking a 2D List containing boolean values, True and False we are filling every element of the 2D list with False as a value. If Sum = 0 is always possible, by taking no elements in the subset. If the value of the current element is less than the sum j, then either include it and the current element or leave it and take the previous best else take the previous best.

SAMPLE INPUT AND OUTPUT

Enter the size of the array: 6 Enter elements of the array: 3 34 4 12 5 2 Enter sum: 9 Subset with given sum is present

Time Complexity: O(sum size of the array) Space Complexity: O(sum size of the array)