-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathquestion5.c
More file actions
21 lines (17 loc) · 776 Bytes
/
question5.c
File metadata and controls
21 lines (17 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
Find a subset in an array whose sum is w
METHOD:
Since number of subsets possible are 2^n, by brute force we can solve it
Better method:
we cannot use greedy here, as it will always pick the smallest or largest number which wont give the
right answer always, hence we use DP.
DP in this case is similar to 0/1 knapsack problem. Therefore we need a sum of S and at any point if
we include the ith element, it will be
S(i,n) = s(i,n-ai) || s(i-1,n) ;n>ai //by taking first i elements is the sum of n possible
//either include it || dont include it
= s(i-1,n) ; n<ai
it will give false if i is 0 (no elements are left) //base condition
it will always give true if sum is zero //base condition
Here also time complexity will be O(nw)
Algo will be same as that of prev.
*/