Subset Sum Problem in O ( sum ) space |  ; initializing with 1 as sum 0 is always possible ; loop to go through every element of the elements array ; to change the values of all possible sum values to 1 ; if sum is possible then return 1 ; Driver code
function isPossible ( elements , sum ) { var dp = [ sum + 1 ] ; dp [ 0 ] = 1 ; for ( var i = 0 ; i < elements . length ; i ++ ) { for ( var j = sum ; j >= elements [ i ] ; j -- ) { if ( dp [ j - elements [ i ] ] == 1 ) dp [ j ] = 1 ; } } if ( dp [ sum ] == 1 ) return true ; return false ; } var elements = [ 6 , 2 , 5 ] ; var sum = 7 ; if ( isPossible ( elements , sum ) ) document . write ( " " ) ; else document . write ( " " ) ;