diff --git a/dynamic_programming/partition.cc b/dynamic_programming/partition.cc new file mode 100644 index 0000000..0a7935c --- /dev/null +++ b/dynamic_programming/partition.cc @@ -0,0 +1,52 @@ +// Partition Problem (Dynamic Programming) + +// Description: +// Given an integer array a, determine whether a can be partitioned into two subsets +// s1 and s2, such that sum(s1) = sum(s2) + +// Example: +// input: arr[] = {4,1,2,3} +// output: True +// The array can be partitioned to {4,1} and {2, 3} + +#include + +using namespace std; + +bool partition(int arr[], int n){ + int sum = 0; + for (int i=0; i= arr[i-1]){ + matrix[i][j] = matrix[i][j] || matrix[i-1][j-arr[i-1]]; + } + } + } + } + return matrix[n][sum]; +} +int main(){ + int arr1[] = {3,1,1,2,2,1}; + int arr2[] = {4,2,1,3,4}; + int arr3[] = {2,4,5,5}; + cout<