def knapSack(A,L,R,K,N,Sum):
if(K==0):
if((L>=Sum)or(R<=Sum)):
return 1
else:
return 0
if((N==0)and(K!=0)):
return 0
else:
return knapSack(A,L,R,K,N-1,Sum)+knapSack(A,L,R,K-1,N-1,Sum:=Sum+A[N-1])
A = [2,4,10,25]
K = 2
L = 3
R = 13
Sum=0
n = len(A)
print(knapSack(A,L,R,K,n,Sum))
The Output Of this Code is: 4
Explanation:
25+10 =35
25+4 = 29
25+2 = 27
10+4 = 14
These Sums satisfies the given condition if((L>=Sum)or(R<=Sum)) where L=3 R=13
K is the size of the subset. Here, K = 2
When K = 3
A = [2,4,10,25]
K = 3
L = 3
R = 13
Sum = 0
n = len(A)
print(knapSack(A,L,R,K,n,Sum))
The Output Of this Code when K = 3 is: 4
Explanation:
4+10+25 = 39
2+4+25 = 31
2+10+25 = 37
2+4+10 = 16
These Sums satisfies the given condition if((L>=Sum)or(R<=Sum)) where L=3 R=13
Is There a way to solve this problem in Dynamic Programming or any other better way?