human
Find all combinations that add upto given number | arr - array to store the combination index - next location in array num - given number reducedNum - reduced number.
Base condition.
If combination is found , print it.
Find the previous number stored in arr [ ] . It helps in maintaining increasing order.
note loop starts from previous number i . e . at array location index - 1.
next element of array is k.
call recursively with reduced number.
Function to find out all combinations of positive numbers that add upto given number . It uses findCombinationsUtil ( ).
array to store the combinations It can contain max n elements.
find all combinations.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to find all combinations that add upto given number and the other steps required.
 def findCombinationsUtil ( arr , index , num , reducedNum ) :
	if ( reducedNum < 0 ) :
		return
	if ( reducedNum == 0 ) :
		for i in range ( index ) :
			print ( arr [ i ] , end = " ▁ " )
		print ( " " )
		return
	prev = 1 if ( index == 0 ) else arr [ index - 1 ]
	for k in range ( prev , num + 1 ) :
		arr [ index ] = k
		findCombinationsUtil ( arr , index + 1 , num , reducedNum - k )
def findCombinations ( n ) :
	arr = [ 0 ] * n
	findCombinationsUtil ( arr , 0 , n , n )
n = 5 ;
findCombinations ( n ) ;