human
Unique subsequences of length K with given sum | Function to find all the subsequences of a given length and having sum S.
Termination condition.
Add value to sum.
Check if the resultant sum equals to target sum.
If true.
Print resultant array.
End this recursion stack.
Check all the combinations using backtracking.
Check all the combinations using backtracking.
Driver code.
Given array.
To store the subsequence.
Function call"
Solution in Python:
assistant
#Here is code to unique subsequences of length K with given sum and the other steps required.
 def comb ( arr , Len , r , ipos , op , opos , Sum ) :
	if ( opos == r ) :
		sum2 = 0
		for i in range ( opos ) :
			sum2 = sum2 + op [ i ]
		if ( Sum == sum2 ) :
			for i in range ( opos ) :
				print ( op [ i ] , end = " , ▁ " )
			print ( )
		return
	if ( ipos < Len ) :
		comb ( arr , Len , r , ipos + 1 , op , opos , Sum )
		op [ opos ] = arr [ ipos ]
		comb ( arr , Len , r , ipos + 1 , op , opos + 1 , Sum )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 4 , 6 , 8 , 2 , 12 ]
	K = 3
	S = 20
	N = len ( arr )
	op = [ 0 ] * N
	comb ( arr , N , K , 0 , op , 0 , S )