Question

In: Computer Science

Michael and Mary are great friends. They like to play games with numbers. This time, Michael...

Michael and Mary are great friends. They like to play games with numbers. This time, Michael has given Mary a list of numbers and given him the task of determining if it is possible to choose a subset of them such that they sum is equal to another given number.

Build an algorithm using dynamic programming to help Mary with his problem.

INPUT

  • The first line corresponds to N, the amount of numbers given by Michael
  • The next N lines contain each of the numbers
  • The last line contains the desired sum

OUTPUT

  • True or False (Whether is possible to obtain the desired sum or not)

Sample Input 1:

39
15
88
3
43
40
100
45
34
89
10
17
70
14
63
92
24
64
6
42
12
81
34
38
63
35
77
67
80
5
58
46
17
67
21
73
10
49
21
54
128

Sample Output 1:

True

Sample Input 2:

30
72
21
33
6
72
53
71
28
66
92
90
73
33
10
70
25
96
82
85
53
58
61
76
20
85
4
1
7
34
76
670

Sample Output 2:

True

Sample Input 3:

35
8
63
89
6
83
78
31
5
46
95
59
73
98
47
43
16
44
50
32
5
77
23
30
44
69
94
24
14
50
99
82
90
1
21
25
557

Sample Output 3:

True

Sample Input 4:

38
2
24
70
51
24
51
98
9
81
55
11
50
94
42
91
94
8
64
86
20
25
34
100
65
30
49
99
47
63
20
3
61
5
100
86
22
94
60
1766

Sample Output 4:

True

Sample Input 5:

21
29
53
31
74
21
57
35
67
10
86
60
46
17
52
38
21
14
51
26
12
86
647

Sample Output 5:

True

Write a program, test using stdin → stdout


**Note: it would be very useful if the code is solved in Python or C++, Thanks**

Solutions

Expert Solution

Python code:

def Subset_sum(arr,n,sum):
#base cases...
if(sum == 0):
return True
if(n == 0):
return False

if(arr[n-1]>sum):
return Subset_sum(arr,n-1,sum)

return Subset_sum(arr,n-1,sum) or Subset_sum(arr,n-1,sum-arr[n-1])


if __name__ == '__main__':
N = int(input())
li = []
for i in range(N):
li.append(int(input()))
s = int(input())
print(Subset_sum(li,N,s))

for every given test cases output is comming True .. u check it again


Related Solutions

Peter and John are great friends. They like to play games with numbers. This time, Peter...
Peter and John are great friends. They like to play games with numbers. This time, Peter has given John a list of numbers and given him the task of determining if it is possible to choose a subset of them such that they sum is equal to another given number. Build an algorithm in Python using brute force to help John with his problem. INPUT The first line corresponds to N, the amount of numbers given by Peter The next...
A group of six friends play some games of ping-pong with these results: Amy beats Bob...
A group of six friends play some games of ping-pong with these results: Amy beats Bob Bob beats Carl Frank beats Bob Amy beats Elise Carl beats Dave Elise beats Carl Elise beats Dave Frank beats Elise Frank beats Amy Consider the relation R = {hx, yi : x has beaten y}. (a) Draw the directed graph G representing R. (b) Is R reflexive? Irreflexive? Symmetric? Asymmetric? Antisymmetric? Transitive? An equivalence? An order? (c) The players want to rank themselves....
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like Games was launched eight years ago, whereas Our Play is a relatively new company that has been in operation for only the past two years. However, both companies have an equal market share with sales of $200,000 each. You’ve collected company data to compare Like Games and Our Play. Last year, the average sales for all industry competitors was $510,000. As an analyst, you...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like Games was launched eight years ago, whereas Our Play is a relatively new company that has been in operation for only the past two years. However, both companies have an equal market share with sales of $200,000 each. You’ve collected company data to compare Like Games and Our Play. Last year, the average sales for all industry competitors was $510,000. As an analyst, you...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like Games was launched eight years ago, whereas Our Play is a relatively new company that has been in operation for only the past two years. However, both companies have an equal market share with sales of $300,000 each. You’ve collected company data to compare Like Games and Our Play. Last year, the average sales for all industry competitors was $765,000. As an analyst, you...
Mary Higgins is a freelance writer with enough spare time on her hands to play the...
Mary Higgins is a freelance writer with enough spare time on her hands to play the stock market fairly seriously. Each morning she observes the change in stock price of a particular stock and decides whether to buy or sell, and if so, how many shares to buy or sell. Assume that on day 1, she has $100,000 cash to invest and that she spends part of this to buy her first 500 shares of the stock at the current...
You are analyzing two companies that manufacture electronic toys - Like Games Inc. and Our Play...
You are analyzing two companies that manufacture electronic toys - Like Games Inc. and Our Play Inc. Like Games was launched eight years ago, whereas Our Play is a relatively new company that has been in operation for only the past two years. However, both companies have an equal market share with sales of $400,000 each. You've collected company data to compare Like Games and Our Play. Last year, the average sales for all industry competitors was $1,020,000. As an...
Great Games Company operates miniature golf courses throughout southern Arizona. On July 1, 2016, Great Games’...
Great Games Company operates miniature golf courses throughout southern Arizona. On July 1, 2016, Great Games’ balance sheet showed (all dollar figures in thousands): Cash $40 -- Accounts Payable $50 -- Credit Card Receivables 25 -- Accruals Payable 0 -- Golf Supplies 35 -- Long-term Notes Payable 0 -- Office Equipment (net) 45 -- Common Stock 65 -- Retained Earnings 30 Draft the journal Entry and Balance Sheet During July the following events occurred to Great Games: a) Collected $20...
Great Games Company operates miniature golf courses throughout southern Arizona. On July 1, 2016, Great Games’...
Great Games Company operates miniature golf courses throughout southern Arizona. On July 1, 2016, Great Games’ balance sheet showed (all dollar figures in thousands): Cash $40 -- Accounts Payable $50 -- Credit Card Receivables 25 -- Accruals Payable 0 -- Golf Supplies 35 -- Long-term Notes Payable 0 -- Office Equipment (net) 45 -- Common Stock 65 -- Retained Earnings 30 Draft the Journal Entry During July the following events occurred to Great Games: a) Collected $20 cash on credit...
Michael and Mary Mason sold for $520,000.00 in March of 2019 their residence that they had...
Michael and Mary Mason sold for $520,000.00 in March of 2019 their residence that they had purchased in 2014 for $75,000.00. They had made capital improvements during their 10-year ownership totaling $25,000.00. They moved into a smaller house that cost $220,000.00. A). What is their recognized gain should they elect to use Section 121? _______ _____ B). What is their recognized gain should they elect to use Section 121 if they sold the house for $720,000.00? C). Assume instead that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT