Update EqualSumPartition.java


Partition an integer array in two parts such that sum of each parts are same. The elements need not to be contiguous.
Brute force

If we try to find out all the subsets of the set it is of the order 2^n. For each of this subset we will have to find the sum and check it with the half of the total sum. So the total complexity will become O(n*2^n)
Better Solution


The sum of two parts should be equal. That means sum should be equal to half of total sum. As they are all integers so if the sum is odd number then it is not possible to partition. If they are even we will try to find whether that half of sum is possible by adding numbers from the array. We solve this by dynamic programming. We take a two dimensional array L of size count+1, sum/2+1. Where L[i,j]=maximum sum possible with elements of array 0 to i and sum not exceeding j. We compute this array from L[0,0] to L[n,sum/2]. So for computing L[i,j] we need to check the element i. if it is greater than j then it cannot be included in the sum so L[i,j]=L[i-1,j]. If ith element is less than j, then we should consider two cases and take the greater of them. First case is where we don't include the element in sum. Then L[i,j] = L[i-1,j] which is exactly like before. Now if we include the element, then L[i,j]=a[i]+L[i-1,j-a[i]]. In this way we fill this 2-d array. If L[n,sum/2] is equal to sum/2 that means the array can be partitioned in two parts whose sums are equal. Then we find out which elements have been included in that sum by backtracking through that 2-d array.

For backtracking through the array we just apply the reverse logic. If L[i,j] is equal to a[i]+L[i-1,j-a[i]] then that element is included in the partition. Then we continue this process to L[i-1,j-a[i]). If it is not equal then the element is not included in the final sum, so we proceed to L[i-1,j] After we get the complete list of elements in a partition we remove them from the original array to get the second partition.