• Source
    1. import java.util.*;
    2.  
    3. class Ideone{
    4. public static void main(String[] args){
    5. int set[] = {3,5,4,6,1,7}; //{3,4,6}, {1,7,5}
    6. int n = set.length;
    7.  
    8. int totalSum = 0;
    9. for(int x:set) totalSum+=x;
    10.  
    11. if(totalSum%2 == 1) {
    12. System.out.println("Equal partition not possible as summ is odd");
    13. return;
    14. }
    15.  
    16. //This code has complexity of O(N * SUM) and uses O(N * SUM) extra space.
    17. int sum = totalSum/2; //Find out whether sum can be formed or not
    18. boolean[][] partitionSum = new boolean[n+1][sum+1];
    19.  
    20. //If sum is zero, we don't require to include any element. So default value will be true
    21. for(int i=0; i<n+1; i++){
    22. partitionSum[i][0] = true;
    23. }
    24.  
    25. //If set is empty, we can't get sum without including any element
    26. for(int i=0; i<sum+1; i++){
    27. partitionSum[0][i] = false;
    28. }
    29.  
    30. for(int i=1; i<n+1; i++){
    31. for(int j=1; j<sum+1; j++){
    32. if(partitionSum[i-1][j] || (j >=set[i-1] && partitionSum[i-1][j-set[i-1]])){
    33. partitionSum[i][j] = true;
    34. }
    35. }
    36. }
    37.  
    38. if(partitionSum[n][sum]){
    39. System.out.println("Equal partition possible");
    40. }else{
    41. System.out.println("Equal partition not possible with given input set");
    42. }
    43. }
    44. }
    45.