Subset Sum Problem Dynamic Programming

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • Given a set of non negative numbers and a total, find if there exists a subset in this set whose sum is same as total.
    github.com/mis...
    github.com/mis...

ความคิดเห็น • 367

  • @bhavuks6554
    @bhavuks6554 6 ปีที่แล้ว +306

    Turn on closed captions and see what youtube translates Tushar's name into at 0:02

    • @vimalk8923
      @vimalk8923 6 ปีที่แล้ว +1

      :)

    • @pranaytanniru7764
      @pranaytanniru7764 5 ปีที่แล้ว +1

      that's funny :P

    • @skullTRTR
      @skullTRTR 4 ปีที่แล้ว +69

      for people don't want to go back to 0:02. "Hello friends my name is too short"

    • @cswalker21
      @cswalker21 4 ปีที่แล้ว +2

      that's messed up :)

    • @123chen9
      @123chen9 4 ปีที่แล้ว +1

      funny things..haha

  • @joecoke6925
    @joecoke6925 7 ปีที่แล้ว +11

    The way you tediously run through the grunt work of the algorithms is really effective for me. Thank you for doing this for free.

  • @GuyMorita
    @GuyMorita 4 ปีที่แล้ว +35

    New name for the channel: "Yes, we'll use Dynamic Programming"

  • @kalpstream
    @kalpstream 8 ปีที่แล้ว +503

    I think your explanations are not very intuitive. You completely skip the idea of breaking problem in to subproblem and constructing solution from optimal solution of its subproblems. Instead of going through the tedious table construction exercise maybe you should talk more about how to figure out subproblems, the core logic.

    • @kalpstream
      @kalpstream 8 ปีที่แล้ว +9

      Thanks for taking it constructively. Keep up the good work!

    • @hoelefouk
      @hoelefouk 8 ปีที่แล้ว +13

      true. talk more about the concept and the logic.

    • @nagarjuna119
      @nagarjuna119 8 ปีที่แล้ว +10

      Agree, why you move back no of steps, why you take it from top. focus on the core logic

    • @edwinalvarez8973
      @edwinalvarez8973 7 ปีที่แล้ว +21

      The reason he's going back is because you're subtracting j from i. Think of it this way, let's call the matrix T, now notice how every time T[ i, j ] == i he always goes back to the first column and end up being true since the whole column it's true, this is because you up once because you already used the value at i, now you subtract j - i (j being the sum and i being the current value) you will end up with 0, and 0 always equals to true. This makes sense since you will always be able to come up with the sum of a number that has that number itself in the set of values ex (sum = 8, set: 2, 3, 7, 8, 10).
      Now that's what he's doing everything he goes back i times, he first goes up, subtracting the value already used, then he goes back i times to see if the remainder from the subtraction has a sum from the remaining values that are left. Ex. (sum = 11, set: 2, 3, 7, 8, 10) lets say we on i = 8, row 8, we do subtraction: (sum = 3, set: 2, 3, 7, 10) since we used 8 already we take it off the list (going up one) and then sum is now 3 since we subtracted 8 from 11 (you go back 8 times) and this will return true since theres a 3 in the set of values and the sum itself is three. I hopes this helps.

    • @prakharbansal1
      @prakharbansal1 7 ปีที่แล้ว +1

      Doesn't this algo assume the array to be sorted?

  • @ShahidNihal
    @ShahidNihal 5 ปีที่แล้ว +109

    "How do you solve this problem? Yes, we'll use Dynamic Programming."

  • @niintyhma
    @niintyhma 6 ปีที่แล้ว +19

    For easier backtracking i'd add one more row in the beginning (element 0), that pretty much guarantees you always end up in the upper left corner without worrying about index out of bound issues and checks.

  • @xxbighotshotxx
    @xxbighotshotxx 8 ปีที่แล้ว +4

    Note: When I ran the logic presented at the end of this video in java. I got an arrayindexoutofbounds exception. The problem here is that j < input [j] should be changed to j < input[j-1]

  • @ramo13121
    @ramo13121 7 ปีที่แล้ว +4

    Thanks alot, this really helped me. By you showing how to calculate the values T[i][j] I could finally understand what the idea was.

  • @deepdive429
    @deepdive429 ปีที่แล้ว

    There was direct solution provided for the tabulation solution but I couldn't able to grasp how actually it's working.
    Now everything is clear to me after your explanation. Thanks a lot.

  • @ansrhl9448
    @ansrhl9448 6 ปีที่แล้ว +7

    This approach is great but has a high space complexity (O(w*n) where w is the number of bits you use to represent the required sum and n is the number of items in the list). I tried using a adjacency list instead where the key is the value of the required sum at every step (basically the columns in this matrix) and the value is the list of items which satisfy the sum. I think that it has a lower space complexity. Also, algorithms like these usually have pseudo-polynomial time complexity. Meaning, that the overall time complexity depends on how you represent the total sum required. For example, if the required sum is 100, we are gonna have 0-100 indexes in columns or keys (in my approach). Anyway, thanks for taking your time out and explaining this stuff Tooshar :D

  • @pratimaupadhyay4939
    @pratimaupadhyay4939 6 ปีที่แล้ว +29

    Sir
    If you start by explaining the recursive solution to any problem, then it would be quite easy for beginners to understand the concept of DP more effectively . Some questions that were left unanswered in the videos like, why are we copying the value from the above cell or why are we moving T[i] steps back can be best explained by the recursive solution.

    • @cool_guy_Vaibhav
      @cool_guy_Vaibhav 2 ปีที่แล้ว +3

      congo u got job in Microsoft

    • @ultimatecreed5144
      @ultimatecreed5144 2 ปีที่แล้ว +1

      @@cool_guy_Vaibhav 🤣🤣🤣🤣

    • @cool_guy_Vaibhav
      @cool_guy_Vaibhav 2 ปีที่แล้ว

      @@ultimatecreed5144 Bro she really got job in Microsoft

    • @shakuntalakamade
      @shakuntalakamade ปีที่แล้ว

      @@ultimatecreed5144 he is apeaking truth bro

  • @himanshukaushik948
    @himanshukaushik948 4 ปีที่แล้ว +55

    You explained how this algorithm works, not why this algorithm works. You taught how to do the problem, not how to build logic for such problems.

    • @umairalvi7382
      @umairalvi7382 3 ปีที่แล้ว +2

      Yeah he just told how to fill the table and nothing else

    • @kingwillbeking3110
      @kingwillbeking3110 3 ปีที่แล้ว +4

      Bruh watch Abdul Bari. He is the legend of algorithms.

  • @user-hg9zo5gu7v
    @user-hg9zo5gu7v ปีที่แล้ว

    Wow, this made my life much easier. Thank you! I was noticing the pattern in the 2d matrix, but was not understanding why this pattern made sense.

  • @iliassti4246
    @iliassti4246 6 ปีที่แล้ว +9

    You're the only one that understands what you're talking about. You start filling up a table base on an algorithm you didn't explain

  • @BackToBackSWE
    @BackToBackSWE 5 ปีที่แล้ว +4

    If you are a software engineer...
    you are awesome

    • @thanga2317
      @thanga2317 5 ปีที่แล้ว +2

      That's why you started your own channel :P All the best..

    • @BackToBackSWE
      @BackToBackSWE 5 ปีที่แล้ว

      @@thanga2317 hahahahaha

  • @rohit-ld6fc
    @rohit-ld6fc 2 ปีที่แล้ว +2

    Directly jumping on the board without telling anything how to approach😂😂😂

  • @cliffordbamert7728
    @cliffordbamert7728 2 ปีที่แล้ว +1

    thank you!!! great tabulation explanation. so easy to understand the code with this video

  • @Vavaxman
    @Vavaxman 3 ปีที่แล้ว

    Thanks for jumping straight into useful explanation instead of spending 10 min talking about obvious stuff like many videos

  • @Borthralla
    @Borthralla 8 ปีที่แล้ว +1

    I am making a minesweeper solver, so I made an algorithm for subsetSum. However, I needed every possible subset, not just whether there existed one, and also needed to have multiple instances of the same value in the set. What I did was I started at the first value in the set, added any solutions to my solutionset, then I added the next number, and added solutions where the rightmost value was present. I saved some time by calculating max/min sum of the set so I can immediately rule out values if they are too high/low. I'm not sure how better this algorithm is that brute force, however.

  • @jamespottex5197
    @jamespottex5197 3 ปีที่แล้ว +2

    Thanks man! U helped me a lot... Please keep making such useful videos for us

  • @ukumasympa7299
    @ukumasympa7299 3 ปีที่แล้ว

    Thanks a lot Tushar, I didn't know until now how to solve subset sum problem with dp, but now I get it !

  • @bipul2138
    @bipul2138 5 ปีที่แล้ว +1

    Thanks man....despite being so busy you took out time to help people like us for free....Highly appreciated!

  • @deepak21903
    @deepak21903 7 ปีที่แล้ว +6

    Always love your videos, Tushar. They are To The Point ! Drive home the point quickly.. Thanks !

  • @yanivgardi9028
    @yanivgardi9028 8 ปีที่แล้ว

    Thanks Tushar, great exercise and lesson.
    i believe that if we would like to modify the problem so repetitions are allowed the basic condition should be modified as follows:
    if (j < input[i])
    T[i][j] = T[i-1][j]
    else
    T[i][j] = T[i-1][j] || T[i][j-input[i]]

    • @egor.okhterov
      @egor.okhterov 8 ปีที่แล้ว

      If elements repeat, then it's not a set :)

  • @VitalyBelenky
    @VitalyBelenky 3 ปีที่แล้ว

    Best explanation without water

  • @rahgurung
    @rahgurung 4 ปีที่แล้ว +3

    If this code doesn't works you then, you should know that the first row should be initialized all False (the row which is even above the (0, 0) here because we need to look upwards each iteration so there should be something to look for in first iteration.

  • @dannyteich9356
    @dannyteich9356 6 ปีที่แล้ว +2

    I really appreciate this. Been trying to figure this out lately, and you explained it very well

  • @prateekgupta3122
    @prateekgupta3122 4 ปีที่แล้ว +5

    So, it is very similar to 0/1 knapsack. instead of values, we need to check for True/False

  • @vivekvishal2775
    @vivekvishal2775 3 ปีที่แล้ว +1

    Thank you u are live savour
    Keep going god bless u

  • @FishWash
    @FishWash 6 ปีที่แล้ว

    Some comments are saying this is unhelpful, but I found this exactly what I needed. But I already understood it conceptually and just needed help figuring out the algorithm, other people might need something different.

  • @chengdurhythm
    @chengdurhythm 8 ปีที่แล้ว +1

    Your videos are so good. it helped me a lot, thank you so much sharing your experience and knowledge

  • @gipaln
    @gipaln 6 ปีที่แล้ว

    If you are a programmer this explanation should be enough for you. If you don't like it, search for another solution which helps you more.

  • @ruchit_kadakia
    @ruchit_kadakia 6 ปีที่แล้ว +76

    Guys He is taking out some time from his busy schedule to teach us,. He is trying his best to explain us. Please show some respect and let's not discourage him from uploading other wonderful tutorials.

    • @JohnS-cb4sn
      @JohnS-cb4sn 6 ปีที่แล้ว

      and he should'nt

    • @atishnarlawar1177
      @atishnarlawar1177 6 ปีที่แล้ว +2

      Yup. He works for Apple.

    • @JohnS-cb4sn
      @JohnS-cb4sn 6 ปีที่แล้ว

      If it was up to me, I would leave in the middle of the interview. This is not how you approach a question.

    • @pratikkumar6148
      @pratikkumar6148 5 ปีที่แล้ว

      can u help me with the code , to traverse the matrix and find the relevant elements that add to the specified sum

    • @darshanmange2564
      @darshanmange2564 5 ปีที่แล้ว

      well said

  • @3FortGamerZ
    @3FortGamerZ 5 ปีที่แล้ว +1

    This solution requires the array to be sorted which is not typically specified in the problem.

  • @siddhanttyagi6653
    @siddhanttyagi6653 3 ปีที่แล้ว

    thank you sir. i am struggling in dynamic programming . after watching your video , i finally easily understand the concept that how the dynamic programming works

  • @JaspreetSingh-oo7ry
    @JaspreetSingh-oo7ry 7 ปีที่แล้ว +2

    Extremely easy way to make things understandable...I had seen your video about 0/1 Knapsack and the concept is exactly the same that is been applied here...your effort is highly appreciated and my suggestion to others, try watching 0/1 Knapsack video first and then come back. You might be able to relate and understand it well. Thank you :)

  • @praenubilus1980
    @praenubilus1980 7 ปีที่แล้ว +2

    It's similar to Knapsack problem. Actually the memo matrix can be optimized to one dimensional array. Hoping the author can add the optimization content in future videos.

  • @shubhamrane2918
    @shubhamrane2918 2 ปีที่แล้ว

    The intuition behind dynamic programming is a recursive definition
    f(i, sum) = f(i+1, sum) OR f(i+i, sum-arr[i])

  • @serhiypidkuyko4206
    @serhiypidkuyko4206 5 ปีที่แล้ว +3

    Hi Tushar,
    Thank you for your video.
    Some thoughts:
    1.You don't need (in code) the matrix - one row is quite enough
    2.Since all info is copied from above the values in current row can be obtained from this same current row
    3.Instead of denoting the corresponding cells with T(rue) it's much better and reasonable to do this with numerous of corresponding term-value.
    For example, the matrix of the video would be:
    [0, f, 2, f, f, f, f, f, f, f, f, f]
    [0, f, 2, 3, f, 3, f, f, f, f, f, f]
    [0, f, 2, 3, f, 3, f, 7, f, 7, 7, f]
    [0, f, 2, 3, f, 3, f, 7, 8, 7, 7, 8]
    [0, f, 2, 3, f, 3, f, 7, 8, 7, 7, 8]
    4.You don't need to do calculations to all the cells - you only need add the value of current row to the values of columns (copied from the above non-negative values)
    For example:
    to get the second row from the first one we need only 2 operations:
    to add 0+3 -> we get 3 in the column 3=0+3
    to add 2+3 -> we get 3 in the column 5=2+3
    to get the third row from the second one we need only 3 operations:
    to add 0+7 -> we get 7 in the column 7=0+7
    to add 2+7 -> we get 7 in the column 9=2+7
    to add 3+7 -> we get 7 in the column 10=3+7
    to add 5+7 -> we get 7 in the column 12=5+7>11 - in fact we don't need this operation
    5.So to get the result we check the column 11.
    Since there we have the value 8 - we go to the left by 8 and obtain 3, then go to the left by 3 and obtain 0.
    So 11=8+3
    Here is the code in python:
    #
    def subset_with_given_sum(li,m):
    a=[0]
    b=[0]+[-1]*m
    for x in li:
    c=[]
    for i in a:
    if i+x0:
    result.append(b[j])
    j-=b[j]
    print('{} -> {}'.format(result,m))
    return result
    print('no such sum -> {}'.format(m))
    return
    def test():
    li=[2,3,7,8,10]
    m=11
    subset_with_given_sum(li,m)
    m=14
    subset_with_given_sum(li,m)
    m=16
    subset_with_given_sum(li,m)
    m=19
    subset_with_given_sum(li,m)

  • @navalkumarshukla9447
    @navalkumarshukla9447 4 ปีที่แล้ว +1

    Thanks buddy, it cleared up the concept for me.

  • @rahgurung
    @rahgurung 4 ปีที่แล้ว

    Good explanation, Tushar.

  • @chagantisubhash
    @chagantisubhash 2 ปีที่แล้ว

    exactly what I was looking for

  • @leharbhandari6852
    @leharbhandari6852 9 ปีที่แล้ว

    All your videos are really amazing! Thanks a lot! :)

  • @it029-shreyagandhi5
    @it029-shreyagandhi5 3 หลายเดือนก่อน

    Thanks a lot !Didn't find anything like this on yt

  • @saneperson929
    @saneperson929 6 ปีที่แล้ว

    I love u man. I passed this subject because of ur explanations

  • @parthverma5855
    @parthverma5855 5 ปีที่แล้ว

    Very nice explanation sir. Your tutorial is very helpful

  • @Hetp111
    @Hetp111 5 ปีที่แล้ว

    These videos are better then most of the other channels, but i still wish you could explain the problems in an intuitive way...

  • @CellerCity
    @CellerCity 2 ปีที่แล้ว +1

    Good illustration

  • @dragoncity133
    @dragoncity133 7 ปีที่แล้ว

    similar to Subset Sum Problemwhich asks us to find if there
    is a subset whose sum equals to target value. For this problem, the
    target value is exactly the half of sum of array.
    Following are the two main steps to solve this problem:
    1) Calculate sum of the array. If sum is odd, there can not be two subsets with equal sum, so return false.
    2) If sum of array elements is even, calculate sum/2 and find a subset of array(video method) with sum equal to sum/2.

  • @D0RASIMA
    @D0RASIMA 3 ปีที่แล้ว

    From Korea, with infinite love....

  • @andreig2946
    @andreig2946 6 ปีที่แล้ว

    Thanks for explaning; the algorithm is fun!

  • @HashDLS
    @HashDLS 9 ปีที่แล้ว +2

    There can be multiple subsets for a particular sum. For example if S={1,2,3} and sum = 3 then possible subsets are {1,2} and {3}. How can we print all of them? Your approach of tracing back will only give a single subset I guess. Other than that, great video and nicely explained :)

  • @ivandrofly
    @ivandrofly 4 หลายเดือนก่อน

    I think it would produce the same output if we just subtract from current to find the index of cell instead of going r-1 (or row above)

  • @soumodipsengupta9902
    @soumodipsengupta9902 ปีที่แล้ว

    Now I am starting to get it!

  • @AjeetKumar-qh6qn
    @AjeetKumar-qh6qn 4 ปีที่แล้ว

    If we look at equations at last, that suggest there is 0th row(didn't show up here in video) filled with false, like 0th column is filled with true value.

  • @dragosc8202
    @dragosc8202 3 ปีที่แล้ว +1

    Thanks a lot! Saved my day!

  • @viencong
    @viencong ปีที่แล้ว +1

    old but it's gold

  • @ramkumarreddy4295
    @ramkumarreddy4295 4 ปีที่แล้ว

    Explaining recursive equations in Dp is major task

  • @sachin_bhandari8470
    @sachin_bhandari8470 6 ปีที่แล้ว +1

    Thank you so much for this . Tutorial.

  • @marvelfan5444
    @marvelfan5444 9 ปีที่แล้ว +1

    Amazing clarity on ur presentations, really helpful, thnx!

  • @valiok9880
    @valiok9880 5 ปีที่แล้ว

    that's literally super intelligent as a solution

  • @mikepusha4745
    @mikepusha4745 8 ปีที่แล้ว +2

    Good job, Thanks for the effort!

  • @HarshRaj-fp6pv
    @HarshRaj-fp6pv 5 ปีที่แล้ว +5

    what if there are more subsets fulfilling sum value... how to trace

    • @cooldudecs
      @cooldudecs 3 ปีที่แล้ว

      You would need to do an exhaustive search. It’s extremely inefficient

  • @mrincodi
    @mrincodi 6 ปีที่แล้ว

    Awesome. Also, if all the interviewer wants is a true-false answer, we can just use an array instead of a matrix.

  • @jahanvikhedwal1107
    @jahanvikhedwal1107 7 ปีที่แล้ว +1

    Thanks a lot.. awesome explanation!

  • @fghjklijk789
    @fghjklijk789 9 ปีที่แล้ว

    Thanks for explaining so well !!!.

  • @quanquan1675
    @quanquan1675 3 ปีที่แล้ว

    Thanks, it helped me to understand the solution

  • @OfferoC
    @OfferoC 2 ปีที่แล้ว

    Thanks great explanation

  • @chandan_prabhakar
    @chandan_prabhakar 4 ปีที่แล้ว

    nice video gautam gambhir

  • @agniswarbakshi7961
    @agniswarbakshi7961 8 ปีที่แล้ว +1

    Great explanation..thanks!

  • @lalitmeghwal75
    @lalitmeghwal75 4 ปีที่แล้ว

    will go three steps back, what a logic!

  • @shivaakrish
    @shivaakrish 5 ปีที่แล้ว +1

    Hi Tushar, Could you please tell me how to print all the available subsets for a given number K.

  • @MarcosCastroSouza
    @MarcosCastroSouza 8 ปีที่แล้ว +1

    very good, thanks

  • @RealRadioWithRishav
    @RealRadioWithRishav 5 ปีที่แล้ว

    Could have also incorporated the logic of including and excluding number explicitly rather than if the element on the upper column is True or not. But thanks for the explanation anyways, it greatly helped.

  • @dalvi_
    @dalvi_ 3 ปีที่แล้ว

    Interesting fact, if you take the set of numbers corresponding to a state in US, and take sum to be 269, you can calculate number of ties in the US election!

  • @travelspurs
    @travelspurs 6 ปีที่แล้ว +1

    Beautiful explanation sir..👍✌ wondering since days how this algo works.😊 thanks a lot sir.

  • @pritamchakraborty7163
    @pritamchakraborty7163 9 ปีที่แล้ว +3

    Is there any way we can also display the subset?

  • @kushtolia6081
    @kushtolia6081 11 หลายเดือนก่อน +1

    Legend🔥

  • @ilmctmp
    @ilmctmp 8 ปีที่แล้ว +8

    Hi Tushar,
    Your videos are very helpful in understanding the "working" of the algorithm. If you could explain why the recurrence relation was formed or why a 2D matrix with j 1...sum was used, I would love that. While trying to solve on my own, I used a 1D matrix and couldn't find the solution. After seeing the 2D matrix and recurrence relation, I could solve it. So learning how to intuitively decide on the strategy will be extremely helpful for me.

    • @malharjajoo7393
      @malharjajoo7393 8 ปีที่แล้ว +2

      I might have a small explanation to your question based on my little knowledge.
      I have observed the 2D approach being taken when there is something finite , like the no. of elements here
      can be used only once, Even in the case of the LCS problem , the string is finite .. in case of knapsack , the items are finite.

    • @wireshark572
      @wireshark572 5 ปีที่แล้ว

      Try to solve it with recursion first and then look for the parameters that are changing when you are calling recursion for smaller sub problems... for example in recursion solution for this problem size of the array and value of k both are changing hence it’s 2D dp .. finding a recursion solution and coming up to this conclusion is much more intuitive ..

  • @brockobama257
    @brockobama257 ปีที่แล้ว

    Isn’t it better to sort O(nlogn) then use the sliding pointers technique? Two pointers each pointing at one end of the array, move pointers according to sum being greater or less than the desired sum.

    • @Daddy30011
      @Daddy30011 ปีที่แล้ว

      dp always op. dp sabka baap hai

  • @chandlerzhang9097
    @chandlerzhang9097 8 ปีที่แล้ว

    Thank u for your helpful video!

  • @harrypounds456
    @harrypounds456 3 ปีที่แล้ว +1

    What is the input size for our example 11 + 5?

  • @ali-cu1ne
    @ali-cu1ne 6 ปีที่แล้ว

    Thanks alot for all videos 😚

  • @ashrafulfuad2967
    @ashrafulfuad2967 5 ปีที่แล้ว

    Thank you so much from Bangladesh

  • @gabrissk02
    @gabrissk02 8 ปีที่แล้ว +1

    Tushar, thanks for another great video! I have a question: do the numbers have to be in ascending order? And does it work when you have duplicated numbers. Thanks again.

    • @gabrissk02
      @gabrissk02 8 ปีที่แล้ว

      +Tushar Roy Thanks for the quick reply! My last question: if i found that a T[i][total] is TRUE, even if the algorithm isn't finished, can I return true already?

    • @gabrissk02
      @gabrissk02 8 ปีที่แล้ว

      +Tushar Roy Thank you very much, you've been really helpful. Keep up with these great videos.

  • @Od253
    @Od253 4 ปีที่แล้ว +1

    This is not easy to follow. We should be understanding WHY we need to fill out the table in that way. That isn't really explained here.

  • @stan6458
    @stan6458 4 ปีที่แล้ว

    if I have 6,4,3,2 and 1 in the array, then won't [6,5], [5,4,2], [5,3,2,1] also be subsets, if we backtrack like that, I don't think we can get proper solutions.

  • @dhineshsattia7361
    @dhineshsattia7361 6 ปีที่แล้ว

    Great explaination .

  • @dwstyagi
    @dwstyagi 4 ปีที่แล้ว +1

    am I the only one who thinks this problem is similar to total unique ways to exchange coin with a given amount

  • @DeepShahmusic
    @DeepShahmusic 3 ปีที่แล้ว

    It is same as "coin change problem" in dynamic programming

  • @ganapatibiswas5858
    @ganapatibiswas5858 4 ปีที่แล้ว

    I think top-down technique would be more easier to understand..
    but this was a good video thanks :)

  • @nitinpandey9876
    @nitinpandey9876 4 ปีที่แล้ว

    Nice explanation. Regarding the algo, I don't like the idea of allocating array of the size of sum. Assume 2 numbers, 4mn and 6mn and sum of 10mn, array of size 10mn will cause issues. Is there a different data structure which can be used here ?

  • @talhasen4806
    @talhasen4806 4 ปีที่แล้ว +3

    You just memorized the solution bro.

  • @RajiurRahman1
    @RajiurRahman1 2 ปีที่แล้ว +1

    Hi Tushar, thanks for the video. I was wondering, is there any way to tell the number of possible subsets that make up the sum from this table?

  • @reyanshmishra2019
    @reyanshmishra2019 3 ปีที่แล้ว

    can anyone share the code where we print the subsets too along with true or false in this same code shared by Tushar sir?

  • @chufanchen6073
    @chufanchen6073 5 ปีที่แล้ว

    Brilliant method!

  • @sandeepannits86
    @sandeepannits86 8 ปีที่แล้ว +1

    How to identify whether there are multiple subsets which can produce the given sum? Also, how to find the elements constituting each subset?

    • @bogdyee
      @bogdyee 8 ปีที่แล้ว

      that's a classic knapsack problem try to find it, it will be pretty usefull

  • @abhishekaditya1506
    @abhishekaditya1506 4 ปีที่แล้ว +1

    You should explain why we need to do this instead of how we need to do this.
    The way you explained DP in this video, it will only force the viewers to memorize the procedure which is not the best way to learn algorithms.

  • @rohitnegi4651
    @rohitnegi4651 4 ปีที่แล้ว

    i didn't get the essence of dp.. if this problem came up on interview, idk how would i have solved right then. i mean how to come up with the solution.. that's the main issue, mere solution is not enough

  • @anupamawasthi12
    @anupamawasthi12 7 ปีที่แล้ว

    At 4:31, do we have to go one row above and move 7 steps back to check if the value is true or not, whats the problem of checking it on the same row? As the values from the above row will be copied to the subsequent rows if the element is larger. for example for 3rd row, all the values from 2nd row from column 1 to column 6 are copied.