Generate all unique partitions of an integer | Set 2 | Array to store the numbers used to form the required sum ; Function to print the array which contains the unique partitions which are used to form the required sum ; Function to find all the unique partitions remSum = remaining sum to form maxVal is the maximum number that can be used to make the partition ; If remSum == 0 that means the sum is achieved so prvar the array ; i will begin from maxVal which is the maximum value which can be used to form the sum ; Store the number used in forming sum gradually in the array ; Since i used the rest of partition cant have any number greater than i hence second parameter is i ; Driver code
var dp = Array . from ( { length : 200 } , ( _ , i ) => 0 ) ; var count = 0 ; function print ( idx ) { for ( var i = 1 ; i < idx ; i ++ ) { document . write ( dp [ i ] + " " ) ; } document . write ( ' ' ) ; } function solve ( remSum , maxVal , idx , count ) { if ( remSum == 0 ) { print ( idx ) ; count ++ ; return ; } for ( var i = maxVal ; i >= 1 ; i -- ) { if ( i > remSum ) { continue ; } else if ( i <= remSum ) { dp [ idx ] = i ; solve ( remSum - i , i , idx + 1 , count ) ; } } } var n = 4 , count = 0 ; solve ( n , n , 1 , count ) ;