Partition problem | DP | Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; Calculate sum of all elements ; Initialize the part array as 0 ; Fill the partition table in bottom up manner ; The element to be included in the sum cannot be greater than the sum ; Check if sum - arr [ i ] could be formed from a subset using elements before index i ; Driver code ; Function call
function findPartiion ( arr , n ) { let sum = 0 ; let i , j ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; let part = new Array ( parseInt ( sum / 2 + 1 , 10 ) ) ; for ( i = 0 ; i <= parseInt ( sum / 2 , 10 ) ; i ++ ) { part [ i ] = false ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = parseInt ( sum / 2 , 10 ) ; j >= arr [ i ] ; j -- ) { if ( part [ j - arr [ i ] ] == true j == arr [ i ] ) part [ j ] = true ; } } return part [ parseInt ( sum / 2 , 10 ) ] ; } let arr = [ 1 , 3 , 3 , 2 , 3 , 2 ] ; let n = arr . length ; if ( findPartiion ( arr , n ) == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;