The_Elephant's blog

By The_Elephant, 22 months ago, In English

This is one of the new added problem for dp(dynamic programming) on cses.

Task : Link

It is similar to 0/1 knapsack or subset sum equal to k. I have solved this problem using a single array.

TC : O(N*M)
SC : O(M)

Solution
Code
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by The_Elephant (previous revision, new revision, compare).

»
13 months ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

Your way of skipping the last element (n) is also good ... If we include it then just find no. of ways to create sum/2 i.e., int ans = dp[sum/2], then output ans / 2 (take care of modulo)
Why ? Since for every possible set that set and its compliment will both contribute to dp[sum/2] but we need it only once (choosing one set automatically creates the other set)!

Code