Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partition Problem (Dynamic Programming) added #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions dynamic_programming/partition.cc
Original file line number Diff line number Diff line change
@@ -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 <iostream>

using namespace std;

bool partition(int arr[], int n){
int sum = 0;
for (int i=0; i<n ;i++){
sum += arr[i];
}
if (sum % 2 != 0){
return false;
}
sum /= 2;
int matrix[n+1][sum+1];
for (int i=0; i<=n; i++){
for (int j=0; j<=sum; j++){
if (j==0){
matrix[i][j] = true;
}
else if (i==0){
matrix[i][j] = false;
}
else {
matrix[i][j] = matrix[i-1][j];
if (j >= 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<<partition(arr1, sizeof(arr1)/sizeof(arr1[0]))<<endl;
cout<<partition(arr2, sizeof(arr2)/sizeof(arr2[0]))<<endl;
cout<<partition(arr3, sizeof(arr3)/sizeof(arr3[0]))<<endl;
return 0;
}