DP 15. Partition Equal Subset Sum | DP on Subsequences

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ก.พ. 2022
  • Lecture Notes/C++/Java Codes: takeuforward.org/data-structu...
    Problem Link: bit.ly/34iIIsH
    Pre-req for this Series: • Re 1. Introduction to ...
    a
    Make sure to join our telegram group for discussions: linktr.ee/takeUforward
    Full Playlist: • Striver's Dynamic Prog...
    In this video, we solve the problem of Partition Equal Subset Sum. This problem is the second problem on DP on Subsequences Pattern. Please watch DP 14 before watching this.
    If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.org/interviews/s...
    You can also get in touch with me at my social handles: linktr.ee/takeUforward

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

  • @takeUforward
    @takeUforward  2 ปีที่แล้ว +110

    I need your support, and you can do that by giving me a like, and commenting "understood" if I was able to explain you.
    Keeping a like target of 500 ❤✌🏼

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

      @Striver , can you please a Linked List playlist? This is the most confusing topics for interview preparation i think..

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

      yes i did

    • @thisismr900
      @thisismr900 10 หลายเดือนก่อน

      khub bhalo striver bhaiya, keep striving harddddd

    • @user-lo1pn3bf3u
      @user-lo1pn3bf3u 8 หลายเดือนก่อน

      Sir I have a doubt. if the first sub problem gives true and second sub problem gives false. Then result will be true since we are not passing the sub problem again to find right sub array is equal or not and also the first sub problem is true so we will give true as answer but the result is false. So it is correct?

    • @chanchalroy3417
      @chanchalroy3417 6 หลายเดือนก่อน

      Understood

  • @KunalSingh-kn2ij
    @KunalSingh-kn2ij ปีที่แล้ว +30

    Solved this question without watching the video!
    DP was nightmare for me before watching your playlist.
    Thanks Striver.

  • @guptashashwat
    @guptashashwat ปีที่แล้ว +41

    It is important to check always if(arr[0]

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

      true

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

      @@samualhalder can you explain this condition to me, the prev question worked without with this condition whereas this one won''t

    • @rishabhraj8233
      @rishabhraj8233 8 หลายเดือนก่อน +2

      @@utkarshverma3314 so with this condition we are checking that dp[0][arr[0]] is actually present in dp matrix because the size of row is equal to sum and if we do not check this it will go out of bounds and give error'

    • @shibainu7500
      @shibainu7500 2 หลายเดือนก่อน

      Yea I was also scratching my head to solve the runtime error until I saw the full video and came to know about this condition.

  • @sahilgagan2242
    @sahilgagan2242 2 ปีที่แล้ว +50

    28% done ...... now i feel confident .. THANKS striver bhaiya for this ..... i pray u will achieve everything u want ..... god will bless u always .... u help student who cant afford courses ....

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

      I hava doubt please help !
      In memoization technique , when to declare a 2d array and when to declare 1d array to store previous answers.
      Like I cannot find out which type of array should I declare .
      Thanks.

    • @user-qo6vg5jb6y
      @user-qo6vg5jb6y ปีที่แล้ว +2

      @@abdulnafe6442 if there are two variable change in basic recursion like findsum(n-1,k-1) then 2d dp else if there is only one variable change in recursive function like fibonaci(n-1) then 1d dp

    • @23cash86
      @23cash86 ปีที่แล้ว

      @@abdulnafe6442 after writing recursive code, look at all parameters, if values of only 1 parameter(eg.index) keeps changing it is 1d dp, declare 1d array ... if two parameters change (e.g. index,target) then 2d array should be declared.

    • @ok-jg9jb
      @ok-jg9jb ปีที่แล้ว +1

      @@abdulnafe6442 Don't try to write memorization first try to do recursion and see what are the parameters that are changing and make memorization. You will get it

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

    Your video makes the tough one's look so easy. When I started with this problem, I was confused on how to proceed with this. Once, I saw the explanation, got to know that this is a new version of previous question. Thanks!!

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

    Thanks Striver. Understood.
    I think it is better to take an unordered_map instead of vector. Like this:
    unordered_map prev, curr;
    instead of
    vector prev(k + 1, false), curr(k + 1, false);
    because target value 'k' can be really large and cause lot of space wastage.

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

    you are a gem to the community bro plz continue this trend after following your videos I'm improving logic building the way you teach and the efforts you are keeping are just amazing expecting a like from you :)

  • @shivangmishra1978
    @shivangmishra1978 2 ปีที่แล้ว +20

    You have a very long life I picked up my phone after doing some dp question to check if Bhaiya posted video or not and daam immediately i received notification of your video 😍😌sukoon

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

    understood,mapping of new problem with the problems we have already solved is very much important

  • @preetisahani5054
    @preetisahani5054 9 หลายเดือนก่อน +4

    Understood. Awesome explanation! thought hard but still didn't come up with your logic. You made it so simple.

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

    Thanks Striver !!!
    For such wonderful DP series

  • @k-AnishChatti
    @k-AnishChatti 2 ปีที่แล้ว +1

    Understood !!!! Finally understanding Subset with DP and now I am able to solve Knapsack at 5AM 😁

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

    Whatever you teach it's just osam .Understood .Thanks for this playlist.

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

    Understood 💯💯 Great Explanation. Thank you very much for all you efforts🔥🔥

  • @stith_pragya
    @stith_pragya 6 หลายเดือนก่อน +1

    UNDERSTOOD..............Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

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

    understood it very well
    thanks for this amazing DP series

  • @junaidkhalidi-mw1zs
    @junaidkhalidi-mw1zs ปีที่แล้ว +1

    At end u added a if condition but I think its just :
    if(arr[0]==k)prev[arr[0]]=true .
    It should not be if(arr[0]

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

    Great explanation didn't even need to see the whole video just saw the first 3 minutes and I was like it is too easy .

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

    Best playlist of DP, that can ever exist anywhere

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

    Understood it clearly. Thank you so much.

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

    Best explanation. understood , hope this channel reaches more people

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

    why we are checking arr[0]

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

    Hey understood.
    One question is: let's say our array was [1,1,2,3] and target = 4.
    In the base condition dp[o][arr[0]] = true. We are checking if(arr[0]

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

      dp[0][arr[0]] means that if you're at index 0 and the target that you have is equal to the value of arr[0] then it's true. It basically tells that when you're at index 0 and if the target is 1 in our case (which is arr[0]) then mark it as true.

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

    without watching this video I have solved the question. easy tha bas sum/2 krna hga. if sum odd return false else subsetsum with sum/2. thank you so much Striver bhaiya

  • @user-hz2nh8kg6x
    @user-hz2nh8kg6x 9 หลายเดือนก่อน +1

    Understood and thank you so much Striver ❤

  • @SD-vk3ko
    @SD-vk3ko ปีที่แล้ว

    Hey Striver... THANK YOU So much for all the efforts.
    I just wanted to know, what accessories you use to make the video?

  • @Sumit-wy4zp
    @Sumit-wy4zp 8 หลายเดือนก่อน

    Understood ++;
    Great Explanation .. This is the greatest playlist on Earth.
    Day 152 ..

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

    Understood. Thanks for creating the playlist.

  • @Amitkumar-yh2uw
    @Amitkumar-yh2uw 9 หลายเดือนก่อน +1

    cool content, very crisp and clear (Y)!

  • @MukeshKumar-cc3uh
    @MukeshKumar-cc3uh 5 หลายเดือนก่อน

    Loving the playlist. "Understood" Sir Striver.❤

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

    UNDERSTOOD... !
    Thanks striver for the video... :)

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

    Striver, can you gather the indices for one valid solution from the tabulated dp table ? As one question is to return a solution subarray?

  • @paveshkanungo6338
    @paveshkanungo6338 8 หลายเดือนก่อน

    understood! Thank you Striver!

  • @well....7751
    @well....7751 2 ปีที่แล้ว +4

    I got a different recursion logic , although its a bit lengthy. Here each value can be included in subset 1 or in subset 2.So we write recursion for it and try to find wether for any subsets both of their sums are equal or not.

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

      i also did the same thing but i am not able to memoize it can u tell me what did u do?

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

      Yes same.. but how to create tabulation for the same any idea on that??

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

      @@vaishnavi9755 You can memoize/tabulate with the help of 3D Dp, dp[index][sum1][sum2], but the constraints are too high, it'll give you memory limit exceeded.

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

      @@sayakghosh5104 Okay.. Got it.. Thanks for the answer!

    • @rishabhinc2936
      @rishabhinc2936 10 หลายเดือนก่อน

      i tried using what u said but its giving me runtime and tle .can u help find error
      bool f(int arr[],int n ,int sum1 ,int sum2,vectordp)
      {
      if(n==0)
      {
      return abs(sum1-sum2)==arr[0];
      }
      // pick
      if(dp[n][sum1][sum2]!=-1)
      {
      return dp[n][sum1][sum2];
      }
      bool pick = f(arr,n-1,sum1+arr[n],sum2,dp);
      // not pick
      bool not_pick = f(arr,n-1,sum1,sum2+arr[n],dp);
      return dp[n][sum1][sum2]=(pick || not_pick);
      }

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

    Understood, sir. Thank you very much.

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

    understood
    Also, thank you for the song at the end. It's a nice song and has been added to my playlist 😂

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

    understood, amazing explanation.

  • @Hrushi_2000
    @Hrushi_2000 10 หลายเดือนก่อน

    Understood. Thankyou Sir.

  • @udaytewary3809
    @udaytewary3809 10 หลายเดือนก่อน +1

    Understood bhaiya ❤❤❤
    I solved this question on my own bhaiya 🎉🎉 really happy 😊😊
    And this is only possible is because of u❤❤

  • @prakhardixit1597
    @prakhardixit1597 9 หลายเดือนก่อน

    In the current problem, would the arr[i] be greater than the target only if we have negative elements? Since we are obtaining the target by summing the elements up?
    If there are negative elements in the array, the range of target in the defined dp will have to change to 2 * target + 1, correct? And we will need to offset each sum value value by adding target to it to make it within the positive range of [0, 2*target]?

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

    Thankyou so much Striver

  • @aps8874
    @aps8874 18 วันที่ผ่านมา

    Thank you so much!

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

    Understood , awssmm Videos (your DP series is LIT !!)

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

    bro doing gods work

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

    bro able to solve it myself ... with exactly same way u explained .... looks like I started thinking like legend :D hhehehe

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

    @takeUforward
    Striver, your classes are amazing . Please keep on going......💥💥💥💥💥💥.
    I have a doubt like what if array contains arr = [100] ?? ?
    does the above code support above test case?

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

    At 8:33 why using condition if( arr[0] == k ) instead of if(arr[0]

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

      It has to be

    • @Divyendu-by7te
      @Divyendu-by7te ปีที่แล้ว

      @@nithish_raina can you please explain this to me?

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

    What if we have given divide in to K subset ?

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

    understood at its peak

  • @gunduboinadileep9523
    @gunduboinadileep9523 8 หลายเดือนก่อน

    understood, Sir!

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

    Is way of finding subsequence and subset are same pick and not pickup?

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

    I hava doubt please help !
    In memoization technique , when to declare a 2d array and when to declare 1d array to store previous answers.
    Like I cannot find out which type of array should I declare .
    Thanks.

  • @pankajmittal1002
    @pankajmittal1002 2 หลายเดือนก่อน

    understood!!

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

    You r genius man...U r gem

  • @gunjjoshi5687
    @gunjjoshi5687 9 หลายเดือนก่อน

    good observation

  • @himanshukhurana9357
    @himanshukhurana9357 7 หลายเดือนก่อน

    understood!

  • @jyothiyadav2595
    @jyothiyadav2595 9 หลายเดือนก่อน

    Understoooood ❤❤❤❤❤❤❤❤

  • @prabhakaran5542
    @prabhakaran5542 5 หลายเดือนก่อน +1

    Understood ❤

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

    You are a SUPERHERO 🧡

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

    Understood Thank you so much :)

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

    can we do it if no target was given just equal sum subset

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

    Understood as always. Thanks

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

    solved by just getting hint in the first half thanks bhaiya

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

    Understood, thanks!

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

    Understood!!Thank you

  • @Yoshitha-fq9en
    @Yoshitha-fq9en 7 หลายเดือนก่อน

    Really Nice

  • @saurabhrthakur
    @saurabhrthakur 3 หลายเดือนก่อน

    Understood!

  • @user-ju6wu3iu8b
    @user-ju6wu3iu8b 9 หลายเดือนก่อน

    Understood👍👍

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

    understood Sir Thank you very much

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

    understood. And thank you.

  • @AbhishekKumar-cv1dh
    @AbhishekKumar-cv1dh 8 หลายเดือนก่อน

    Understood🔥🔥🔥🔥

  • @nithishlelll9664
    @nithishlelll9664 7 หลายเดือนก่อน +1

    Understood!!😄

  • @pragyanandsingh6673
    @pragyanandsingh6673 6 หลายเดือนก่อน

    Understood !!

  • @surbhirathore._
    @surbhirathore._ 22 วันที่ผ่านมา

    Understood❤

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

    Understood, thanks a ton

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

    understood well bro

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

    The question might have been more clear by specifying (S1 U S2 ) = A
    where Si is a subset and A is the original array.

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

      Correct! In the question they've not said that both subset should make up the entire array

  • @DRAGONGODX10
    @DRAGONGODX10 2 หลายเดือนก่อน +1

    understood

  • @utkarshverma3314
    @utkarshverma3314 11 หลายเดือนก่อน

    how exactly is the condition if(arr[0]

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

    Understood...Completed 15/56

  • @DevashishJose
    @DevashishJose 7 หลายเดือนก่อน

    understood.

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

    Please make a video on "Partition to K equal Sum subsets"........i can't find any dp solution understandable. Please please please make a video on its dp approach god please. Humble request bhaiya, please. Btw understood 🙏.

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

    understood sir thank you sir i love you sir mzaaaaaaa gya padhke ....ab to lg rha h jaisse dp mere bacche jaisa h

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

    understood. thanks

  • @arihantjammar8888
    @arihantjammar8888 9 หลายเดือนก่อน

    Understood 😃😊

  • @SajanKumar-ec2us
    @SajanKumar-ec2us 3 หลายเดือนก่อน

    i did not understood when if a subset is present in array then other equal sum subset will present definitely also discuss is it memoisation problem

  • @santoshb7776
    @santoshb7776 8 หลายเดือนก่อน

    Understood sir

  • @user-up6sl2gq8p
    @user-up6sl2gq8p 5 หลายเดือนก่อน

    TQ

  • @vamsikrishnareddy9345
    @vamsikrishnareddy9345 5 วันที่ผ่านมา

    Understood!!!

  • @Anonymous-uj3jx
    @Anonymous-uj3jx 2 ปีที่แล้ว

    Understood thanks :)

  • @Hard2Smart_Coder
    @Hard2Smart_Coder 9 หลายเดือนก่อน

    Understood😀

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

    Understood.

  • @parthib.1555
    @parthib.1555 7 หลายเดือนก่อน +1

    Understood

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

    8:00 that condition is necessary to pass all test cases for the same question in LeetCode

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

      Not necessary, I have submitted same question on leetcode without this condition.
      Just comment this line
      // if(nums[0]

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

    Great. Understood.

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

    Can anyone help me to write a code to print the K subsets with equal sum..
    Condition is that we can only a element once in a subset..
    Input- 1 2 2 3 3 4 5
    K=4

  • @anirvangoswami
    @anirvangoswami 10 หลายเดือนก่อน

    Maan Gaye guru.sticker

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

    Hey, can you make a video about what should be the size of our DP?
    I'm always confused between n or n+1

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

      Depends, whether you require n states or n+1 states. To be on a safer side, generally ppl declare n+1 size array

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

    But there may be case that, no. of elements are even but subsets elements are not counting to same.
    eg [6,6,4,4,2,2]
    6+6=12(2 elements) and 4+4+2+2=12(4 elements)
    Plz someone explain.

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

    UNDERSTOOD!! 😁