DP Dynamic Programming

How can we find is the sum of exactly k elements from an array of size n is equal to a particular value b?

suppose a[n] = {1, 5, 7, 11, 12}

k = 3
b = 23

res = {1+5+7, 1+5+11, 1+5+12, 1+7+11, 1+7+12, 1+11+12, 5+7+11, 5+7+12, 5+11+12, 7+11+12 }

res = {13, 17, 18, 19, 20, 24, 23, 24, 28, 30}

so b=23 is present in res, then return true

if b is 22 is not present, then return false

@skysinghthakur
hello akash,
a)declare a boolean variable b and intiialse it with false (true means exist false means not exist)
b) iterate from j=1 to 2^n-1
c) check number of set bits in j .if it is equal to k and sum of array values for that j is p then make b=true

d) after iteration check whether b is true or not .if it is true print yes otherwise no

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.