Set partition

Partition problem is to determine whether a given set can be partitioned into two subsets such that the sum of elements in both subsets is same.

C++:
01 bool setPartition(int A[], int size, int sum, int bsum, int checked)
02 {
03     if(bsum*2==sum)
04         return true;
05     if(checked==size)
06         return false;
07     bool p=setPartition(A,size,sum,bsum,checked+1);
08     bool q=setPartition(A,size,sum,bsum+A[checked],checked+1);
09     return p||q;
10 }
Advertisements
This entry was posted in Array and linked list. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s