Skip to content

Commit

Permalink
added chefinsq (gopalgoel19#60)
Browse files Browse the repository at this point in the history
Added codechef problem solution to CHEFINSQ
  • Loading branch information
yashyasviagarwal authored and gopalgoel19 committed Oct 11, 2019
1 parent cc7b31c commit 94994d9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions codechef/chefinsq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#function to call recursive function and return the final solution to it's caller function
def count_sets_dp(arr,total,n,k):
mem={}
return dp(arr,total,n-1,mem,k)

#recursive and dynamic programming function to generate subsequences and find the eligible subsequence
def dp(arr,total,i,mem,k):
key=str(total)+':'+str(k)+':'+str(i)
if key in mem:
return mem[key]
if total==0 and k==0:
return 1
elif total<0 or k<0:
return 0
elif i<0:
return 0
elif total<arr[i]:
to_return=dp(arr,total,i-1,mem,k)
else:
to_return=(dp(arr,total-arr[i],i-1,mem,k-1)+dp(arr,total,i-1,mem,k))
mem[key]=to_return
return to_return
#loop through all testcases and call the count_sets_dp funtion
for t in range(int(input())):
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(a)
b.sort()
min_sum=sum(b[:k])
print(count_sets_dp(a,min_sum,n,k))

0 comments on commit 94994d9

Please sign in to comment.