class Solution { /** Recursive code */ static boolean equalPartition(int arr[]) { int n = arr.length; int sum = 0; for(int ele : arr) sum += ele; // if even sum -- then we can divide if(sum%2 != 0) return false; sum = sum/2; return findSubsetSum(arr, sum, n); } static boolean findSubsetSum(int[] arr, int sum, int n) { // when we reach end of the array, so couldn't found a subset if(n == 0) return false; // when sum becomes 0 that means found a subset if(sum == 0) return true; if(arr[n-1] <= sum) { // pick the element in subset boolean pick = findSubsetSum(arr, sum - arr[n-1], n-1); // do not pick the element in subset boolean notPick = findSubsetSum(arr, sum, n-1); return pick || notPick; } // when current element exceeds given sum, so skip the element return findSubsetSum(arr, sum, n-1); } /** Memoization code */ static boolean equalPartition(int arr[]) { int n = arr.length; int sum = 0; for(int ele : arr) sum += ele; // if even sum -- then we can divide if(sum%2 != 0) return false; sum = sum/2; boolean[][] t = new boolean[n+1][sum+1]; // when array of size will increase but sum needs to always 0, then we can have always empty subset for(int i=0; i