|
| 1 | +/* |
| 2 | +https://www.techiedelight.com/partition-problem/ |
| 3 | +https://www.geeksforgeeks.org/partition-problem-dp-18/ |
| 4 | +
|
| 5 | +To Do - Print the Partitions |
| 6 | +
|
| 7 | +Given a sequence of integers, we need to find if they can be divided into two subsets of equal sum. |
| 8 | +
|
| 9 | +arr[] = {1, 5, 11, 5} |
| 10 | +Output: true |
| 11 | +The array can be partitioned as {1, 5, 5} and {11} |
| 12 | +
|
| 13 | +arr[] = {1, 5, 3} |
| 14 | +Output: false |
| 15 | +The array cannot be partitioned into equal sum sets. |
| 16 | +
|
| 17 | +Main points to solve the problem - |
| 18 | +1. If the sum of all integers of the inout sequence is not even ==> The sequence can not be divided into two subsets of equal sum. |
| 19 | +2. If the sum of all integers of teh input seq is even ==> The sequence can be divided into two subsets of equal sum ==> |
| 20 | +sum of each subset will be (totalSum/2) ==> Find if a subset sums up to (totalSum/2) |
| 21 | +
|
| 22 | +RECURSIVE SOLUTION |
| 23 | +We have two choices include or exclude the current item from the subset |
| 24 | +1. Include current item in the subset and recur for other elements with sum's value reduced by current element |
| 25 | +2. Exclude current item and recur for remaining elements with sum's value as it is. |
| 26 | +
|
| 27 | +The below solution uses array's prefixes |
| 28 | +*/ |
| 29 | + |
| 30 | +public boolean isPartition(int A[], int sum, int current){ |
| 31 | + //we found the subset |
| 32 | + if( sum == 0 ) return true; |
| 33 | + |
| 34 | + //we have exhausted all elements and the sum is still not 0 ie. partition not possible |
| 35 | + else if( n==0 && sum!=0 ) return false; |
| 36 | + else{ |
| 37 | + |
| 38 | + //include the current item in the subset |
| 39 | + boolean include = isPartition(A, sum-A[current-1], current-1); |
| 40 | + |
| 41 | + //exclude the current item |
| 42 | + boolean exclude = isPartition(A, sum, current-1); |
| 43 | + |
| 44 | + //does any of the two choices help us attain a partition? |
| 45 | + return (include || exclude); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +public boolean solvePartition(int A[]){ |
| 50 | + int n = A.length; |
| 51 | + int sum = 0; |
| 52 | + |
| 53 | + for(int i=0;i<n;i++) sum+=A[i]; |
| 54 | + |
| 55 | + //partition not possible |
| 56 | + if(sum % 2 != 0) return false; |
| 57 | + |
| 58 | + else return isPartition(A,sum/2,n); |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +/* |
| 63 | +Time Complexity - O(2^n) |
| 64 | +For each element we are exploring two choices - inclusion and exclusion |
| 65 | +*/ |
| 66 | + |
| 67 | + |
| 68 | +/* |
| 69 | +DYNAMIC PROGRAMMING - BOTTOM UP APPROACH |
| 70 | +The problem is similar to the knapsack problem, where we need to choose items to maximize profit and use maximum capacity of the knapsack. |
| 71 | +Similarly In this problem, we are supposed to chose items. But the constraint/Objective is different. We need to choose items such that the sum of these items is equal to (totalSum)/2. |
| 72 | +*/ |
| 73 | + |
| 74 | +public boolean solvePartition(int A[]){ |
| 75 | + int n = A.length; |
| 76 | + int sum = 0; |
| 77 | + |
| 78 | + for(int i=0;i<n;i++) sum+=A[i]; |
| 79 | + |
| 80 | + //partition not possible |
| 81 | + if(sum % 2 != 0) return false; |
| 82 | + |
| 83 | + else return isPartition(A,sum/2,n); |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +//Notice the code is very similar to knapsack problem code |
| 88 | +public boolean isPartition(int A[], int sum, int n){ |
| 89 | + boolean T[][] = new boolean[n+1][sum+1]; |
| 90 | + |
| 91 | + for(int i=0;i<=n;i++) T[i][0] = true; |
| 92 | + |
| 93 | + for(int i=1;i<=n;i++){ |
| 94 | + for(int j=1;j<=sum;j++){ |
| 95 | + if(A[i-1] > j) T[i][j] = T[i-1][j]; |
| 96 | + else T[i][j] = (T[i-1][j] || T[i-1][j-A[i-1]]); |
| 97 | + } |
| 98 | + } |
| 99 | + return T[n][sum]; |
| 100 | +} |
| 101 | + |
| 102 | +/* |
| 103 | +Time Complexity - O(n*totalSum) |
| 104 | +Space Complexity - O(n*totalSum) |
| 105 | +Number of subproblems is dependent on |
| 106 | +1. number of elmenets in the array ==> n |
| 107 | +2. the subset under consideration --(depends on)--> the target sum(totalSum/2) --(depends on)--> the total sum of all elements |
| 108 | +*/ |
| 109 | + |
| 110 | +//RECUSRION USING MEMOIZATION |
| 111 | +//The below approach uses array's suffixes |
| 112 | + |
| 113 | +public boolean isPartition(int A[], int current, int sum, HashMap<String,Boolean> map){ |
| 114 | + |
| 115 | + //we found the subset |
| 116 | + if( sum == 0 ) return true; |
| 117 | + |
| 118 | + //we have exhausted all elements and the sum is still not 0 ie. partition not possible |
| 119 | + else if( n==0 && sum!=0 ) return false; |
| 120 | + |
| 121 | + String key = current+"|"+sum; |
| 122 | + |
| 123 | + if(!map.containsKey(key)){ |
| 124 | + //include the current item in the subset |
| 125 | + boolean include = isPartition(A, current-1, sum-A[current-1], map); |
| 126 | + |
| 127 | + //exclude the current item |
| 128 | + boolean exclude = isPartition(A, current-1, sum, map); |
| 129 | + |
| 130 | + //does any of the two choices help us attain a partition? |
| 131 | + map.put(key, (include || exclude)); |
| 132 | + } |
| 133 | + return map.get(key); |
| 134 | +} |
| 135 | + |
| 136 | +public boolean solvePartition(int A[]){ |
| 137 | + int n = A.length; |
| 138 | + int sum = 0; |
| 139 | + HashMap<String, Boolean> map = new HashMap<String,Boolean>(); |
| 140 | + for(int i=0;i<n;i++) sum+=A[i]; |
| 141 | + |
| 142 | + //partition not possible |
| 143 | + if(sum % 2 != 0) return false; |
| 144 | + |
| 145 | + else return isPartition(A,sum/2,n); |
| 146 | +} |
| 147 | + |
| 148 | +/* |
| 149 | +Time Complexity - O(n*totalSum) |
| 150 | +*/ |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
0 commit comments