Print all non | Utility function to print array arr [ 0. . n - 1 ] ; Recursive Function to generate all non - increasing sequences with sum x arr [ ] -- > Elements of current sequence curr_sum -- > Current Sum curr_idx -- > Current index in arr [ ] ; If current sum is equal to x , then we found a sequence ; Try placing all numbers from 1 to x - curr_sum at current index ; The placed number must also be smaller than previously placed numbers and it may be equal to the previous stored value , i . e . , arr [ curr_idx - 1 ] if there exists a previous number ; Place number at curr_idx ; Recur ; Try next number ; A wrapper over generateUtil ( ) ; Array to store sequences on by one ; Driver program
def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT def generateUtil ( x , arr , curr_sum , curr_idx ) : NEW_LINE INDENT if ( curr_sum == x ) : NEW_LINE INDENT printArr ( arr , curr_idx ) NEW_LINE return NEW_LINE DEDENT num = 1 NEW_LINE while ( num <= x - curr_sum and ( curr_idx == 0 or num <= arr [ curr_idx - 1 ] ) ) : NEW_LINE INDENT arr [ curr_idx ] = num NEW_LINE generateUtil ( x , arr , curr_sum + num , curr_idx + 1 ) NEW_LINE num += 1 NEW_LINE DEDENT DEDENT def generate ( x ) : NEW_LINE INDENT arr = [ 0 ] * x NEW_LINE generateUtil ( x , arr , 0 , 0 ) NEW_LINE DEDENT x = 5 NEW_LINE generate ( x ) NEW_LINE