Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int FindPartition(int arr[],int n){
- int total=0;
- for(int i=0;i<n;i++)
- total+=arr[i];
- if(total%2!=0)
- return 0;
- total=total/2;
- int sum[n+1][total+1];
- for(int i=0;i<=n;i++)
- sum[i][0]=1;
- for(int i=0;i<=total;i++)
- sum[0][i]=0;
- for(int i=1;i<=n;i++){
- for(int j=1;j<=total;j++){
- sum[i][j]=sum[i-1][j];
- if(arr[i-1]<=j)
- sum[i][j]=sum[i][j]||sum[i-1][j-arr[i-1]];
- }
- }
- return sum[n][total];
- }
- int main(){
- int arr[] = {3,1,1,2,2,1};
- int n = 6;
- if (FindPartition(arr, n))
- cout << "Can be divided into two subsets of equal sum";
- else
- cout << "Can not be divided into"
- << " two subsets of equal sum";
- return 0;
- }
Add Comment
Please, Sign In to add comment