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 print 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
dp = [ 0 for i in range ( 200 ) ] NEW_LINE count = 0 NEW_LINE def print1 ( idx ) : NEW_LINE INDENT for i in range ( 1 , idx , 1 ) : NEW_LINE INDENT print ( dp [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " , end ▁ = ▁ " " ) NEW_LINE DEDENT def solve ( remSum , maxVal , idx , count ) : NEW_LINE INDENT if ( remSum == 0 ) : NEW_LINE INDENT print1 ( idx ) NEW_LINE count += 1 NEW_LINE return NEW_LINE DEDENT i = maxVal NEW_LINE while ( i >= 1 ) : NEW_LINE INDENT if ( i > remSum ) : NEW_LINE INDENT i -= 1 NEW_LINE continue NEW_LINE DEDENT elif ( i <= remSum ) : NEW_LINE INDENT dp [ idx ] = i NEW_LINE solve ( remSum - i , i , idx + 1 , count ) NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE count = 0 NEW_LINE solve ( n , n , 1 , count ) NEW_LINE DEDENT