DP 18. Count Partitions With Given Difference | Dp on Subsequences

  Рет қаралды 176,173

take U forward

take U forward

Күн бұрын

Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
Problem Link: bit.ly/3r8mG5b
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 count partitions with given difference. This problem is the fifth problem on DP on Subsequences Pattern. Please watch DP 17 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

Пікірлер: 592
@takeUforward
@takeUforward 2 жыл бұрын
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 ❤✌🏼
@iamnoob7593
@iamnoob7593 6 ай бұрын
understood
@himalayadebbarma-we4pt
@himalayadebbarma-we4pt 12 күн бұрын
Understood
@AbhishekKumar-cv1dh
@AbhishekKumar-cv1dh 8 ай бұрын
I'll be honest, I was bamboozled with the 0's in array edge case since DP17 and I was simply unable to find a clear answer from the comments. Had I simply closed my eyes and went ahead with DP 18, I would have legit saved ~ 2 hours of confusion!! Thankyou so much Striver, this lecture cleared all my doubts 🔥🔥🔥🔥🔥
@mayanksingh7501
@mayanksingh7501 2 ай бұрын
Those confusion and doubts and 2 hours will only help in longer run. Good work on not directly jumping to another video without clearing your doubts on your own.
@after_dark_777
@after_dark_777 2 ай бұрын
for real should have moved on to this video those comments were so confusing
@SamreenAftab-lg8tx
@SamreenAftab-lg8tx Ай бұрын
Samee
@solitucedagar4298
@solitucedagar4298 17 күн бұрын
sameeeeeeeeeeee
@nikhilmadaan29
@nikhilmadaan29 Ай бұрын
hats off man,, spent 2 hours on DP17 to fix that but after watching this DP 18 everything went super smooth
@saketsoni2587
@saketsoni2587 Жыл бұрын
I studied DP from aditya verma, but was never able to figure out how to handle the zeros in the array, you made it super easy, Thanks!
@entertainmenthub373
@entertainmenthub373 9 ай бұрын
but aditya verma always tell you the in=dentification and where u can use this apparoach nd similar questions he is the god of cp ik he skip the case of 0 in array but aditya verma is for cp where u can use which approach and how to identify which approach by seeing solution
@iamnoob7593
@iamnoob7593 6 ай бұрын
@@entertainmenthub373 God of cp is tourist.
@divyanshrawat2859
@divyanshrawat2859 Ай бұрын
its not like he was not able to figure out , he always gives an intution to solve the problem , not like come and tells you the whole solution.
@cs26divyanshisachan37
@cs26divyanshisachan37 5 ай бұрын
Understood! Hats off to ur dedication, u are still teaching while suffering from fever.
@saurabhsaha6958
@saurabhsaha6958 3 ай бұрын
Striver's concept explanation is so cool, easy and easily get stuck into the head. I wish to meet him one day and say a lot of thanks to him.
@PIYUSH61004
@PIYUSH61004 2 жыл бұрын
The deduction of (totalSum - D) / 2 was amazing. Understood!
@parthsalat
@parthsalat 2 жыл бұрын
Understood 💯💯Great Explanation. Thank you very much for all you efforts🔥🔥
@Aniruddha_Chowdhury
@Aniruddha_Chowdhury 2 ай бұрын
understood until 14:00 ❤ . Will learn the optimization later
@tarunrao1505
@tarunrao1505 Жыл бұрын
Hello, The tabulation code is not passing all test cases in both videos dp-17 && dp-18. Is anyone facing the same problem. Pls HELP.
@narendramaurya4823
@narendramaurya4823 4 ай бұрын
Us, but the way you transformed the problem into previously solved problem is amazing , that's the way we have to think ... Thanks ❤
@ScienceSeekho
@ScienceSeekho Жыл бұрын
understood. Also thanks for explaining Space Optimization so good. I am from tier-3 mechanical never knew all these stuff
@stith_pragya
@stith_pragya 6 ай бұрын
UNDERSTOOD.........Thank You So Much for this wonderful video...............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@tasneemayham974
@tasneemayham974 10 ай бұрын
UNDERSTOOODD!!! Thank you, Striver!!
@gauravgogoi5240
@gauravgogoi5240 3 ай бұрын
After this video, they updated the problem! Real influencer
@MukeshKumar-cc3uh
@MukeshKumar-cc3uh 4 ай бұрын
Understood 👍. Hats off to your dedication for us. ♥
@thatsharma1066
@thatsharma1066 22 күн бұрын
I was confused with the previous video 0 case, but now understood. Thanks.
@chetanthakral5322
@chetanthakral5322 2 жыл бұрын
Another modified target could be (difference + totalSum)/2; To explain this how this came is : s1= sum of elements in subset 1 s2 = sum of elements in subset 2 s1 - s2 = d (We need to find 2 subsets with difference d ) s1 + s2 = totalSum (We know the sum of 2 subsets would be equal to the total sum of array) Adding these 2 equations , we get s1 = (d + totalSum)/2, thus we only need to find a subset with sum s1.
@takeUforward
@takeUforward 2 жыл бұрын
But this will increase the space req :)
@chetanthakral5322
@chetanthakral5322 2 жыл бұрын
@@takeUforward Oh Okay , I didn't think about that !
@varunaggarwal7126
@varunaggarwal7126 Жыл бұрын
@@chetanthakral5322 lol, I thought the same thing, but its not pure math its cs.
@UCSParnashreeDas
@UCSParnashreeDas Жыл бұрын
@@varunaggarwal7126 how this increases the space.. can u explain
@varunaggarwal7126
@varunaggarwal7126 Жыл бұрын
@@UCSParnashreeDas it's been 1 month,lol i have to revise this dp, but I guess you can see + sign while striver is subtraction, means less
@pulkitchausali1354
@pulkitchausali1354 Жыл бұрын
Already understood before starting of video that's what Striver teaches us
@ritikshandilya7075
@ritikshandilya7075 Ай бұрын
Great Explanation Striver , Thanks
@adebisisheriff159
@adebisisheriff159 6 ай бұрын
Understood! Striver. The best Software Engineer himself
@ranasauravsingh
@ranasauravsingh 2 жыл бұрын
UNDERSTOOD... ! Thanks striver for the video... :)
@anveshkarra3861
@anveshkarra3861 2 жыл бұрын
understood , the space optimization is amazing sir
@priyankabagal9269
@priyankabagal9269 2 жыл бұрын
You have literally made dp look so easy !
@shoryapawar2219
@shoryapawar2219 10 ай бұрын
Understood Thanks Striver for this Dp series
@DevashishJose
@DevashishJose 7 ай бұрын
understood. Thank you so much
@shreyashtech8556
@shreyashtech8556 4 ай бұрын
bro doing gods work
@TON-108
@TON-108 9 ай бұрын
Understood Bhaiya!
@hrushikesh-1914
@hrushikesh-1914 10 ай бұрын
Understood. Thankyou sir.
@ratinderpalsingh5909
@ratinderpalsingh5909 2 жыл бұрын
Understood, sir. Thank you very much.
@cinime
@cinime 2 жыл бұрын
Understood! Thank you so so so much!!!
@musichub2168
@musichub2168 2 жыл бұрын
Understood 💯💯Great Explanation
@kushjoshi9716
@kushjoshi9716 Жыл бұрын
Nicely Explained! Understood!
@sonicboom6635
@sonicboom6635 8 ай бұрын
In the space optimized solution why the loop sum=1 to target doesn't work as we have been doing previously and setting curr[0] =1 since for any index with target as 0 there is only single subset. Can someone explain please?
@blackblood181
@blackblood181 Жыл бұрын
we can also use : if(ind < 0){ if(sum==0) return 1; return 0; } apart from these conditions: if(ind == 0) { if(sum==0 || num==arr[0]) return 1; if(sum==0 && arr[0]==0) return 2; return 0; }
@anuragprasad6116
@anuragprasad6116 Жыл бұрын
this makes the code really simple to understand. perfect base case.
@theterror9080
@theterror9080 11 ай бұрын
Best Bhai @@anuragprasad6116
@aps8874
@aps8874 11 күн бұрын
Thank you so much!
@harshitsharma5647
@harshitsharma5647 Жыл бұрын
Superb Bhaiya guys watch at 10:10 Amazing Concept Again and again Thankyou bhaiya Aka Striver
@balajisrinivasan6618
@balajisrinivasan6618 Жыл бұрын
Thank you striver ! Just a note : writing recursion from 0 to n-1 looks far easier to handle bases cases than writing recursion from n-1 to 0 on subsequence problems
@santoshpokhrel7693
@santoshpokhrel7693 9 ай бұрын
but for me it creates problem while writing the tabulation form. Esp. with all the subsequences questions.
@chase.2595
@chase.2595 9 ай бұрын
yeah man, we have to start from n-2 if we do 0 to n-1 in recursion@@santoshpokhrel7693
@ganeshktvb9234
@ganeshktvb9234 6 ай бұрын
yaa bro really it creates confusion in tabulation @@santoshpokhrel7693
@mrinmoyhalder7293
@mrinmoyhalder7293 2 жыл бұрын
hey can anyone please clear my doubt. I'm applying DP-16 concept.like after filling the tabulation with boolean, check and try for every s1, that it's possible to get standing on last index, if possible then calculate s2 and check the condition given in the qsn and increment count, Now this method is not working, I think it's because the tabulation is not recording duolicacy, e.g, In array if the sum 7 is made by more than one way, then it's not taking that record, and while checking for (s1>s2 && s1-s2==d), only once the count is increasing, is it the actual reason of failing ?
@ankanbasu7381
@ankanbasu7381 Жыл бұрын
4:48 or I think, we can go 1 step deeer into indx = -1 then base case would be simpler if (indx == -1) { if (sum == 0) return 1; else return 0; }
@introvert9112k
@introvert9112k Жыл бұрын
Then how do you take care of base case in Tabulation. As dp array cannot have -ve indexes.
@RTXCR7
@RTXCR7 Жыл бұрын
@@introvert9112k for that i think we would need to make 1 indexed arrays of size n+1 having an extra 0 index free
@abhijeetbasfore6816
@abhijeetbasfore6816 2 жыл бұрын
Thank you so much I've understood
@user-lw9dj8we7k
@user-lw9dj8we7k 10 ай бұрын
Understood Sir.
@prabhakaran5542
@prabhakaran5542 4 ай бұрын
Understood ❤
@UECAshutoshKumar
@UECAshutoshKumar Ай бұрын
Understood!
@ramyasree4986
@ramyasree4986 2 жыл бұрын
completed subsequences set problems great learning
@arinmodi8884
@arinmodi8884 2 жыл бұрын
Hey, Striver I understood the logic in this but how are you able to think of using logic like this very quickly ? , i didn't get that by looking at the question.
@shriRadheHariom
@shriRadheHariom 4 күн бұрын
Understood, well explained.
@shubh625
@shubh625 Күн бұрын
ok
@firstac3506
@firstac3506 3 ай бұрын
Understood 🔥🔥
@khushigupta5798
@khushigupta5798 5 ай бұрын
UNDERSTOOD
@drishtirai864
@drishtirai864 4 ай бұрын
Understood !!
@arpitrajput6424
@arpitrajput6424 2 жыл бұрын
In Lecture 17 , we can sort the vector and reverse it, then no need to change base case it will work absolutely fine.
@riddhibandyopadhyay584
@riddhibandyopadhyay584 2 жыл бұрын
Yes, but it will take extra time complexity
@arpitrajput6424
@arpitrajput6424 2 жыл бұрын
@@riddhibandyopadhyay584 ya you're right but can be done ✅ with this approach .👍
@akashkumarsingh3595
@akashkumarsingh3595 2 жыл бұрын
If we have handled the case num[0] == 0 previously then why we are checking it again for num[0]
@gunduboinadileep9523
@gunduboinadileep9523 7 ай бұрын
understood!
@shaiksuhel6505
@shaiksuhel6505 2 ай бұрын
Understood🎉
@rohandevaki4349
@rohandevaki4349 Жыл бұрын
at 15:53 , the target should run from 1 to sum right? we have run from 1 to sum in the count subsequences with sum k also, why did you take from 0 to sum?
@user-zf6cz2th3s
@user-zf6cz2th3s 5 ай бұрын
understood
@user-ve4jm9pj3s
@user-ve4jm9pj3s 21 күн бұрын
another base case(Better and Concised): if(ind
@jk-sm6qr
@jk-sm6qr Ай бұрын
Thank you
@champu5645
@champu5645 2 жыл бұрын
I thing the simplest way to get rid of so many conditions of 0's we can simply start the recursion for 0 to n-1; then we will get correct ans i.e.4;
@nithish_raina
@nithish_raina Жыл бұрын
Yes, and it would also work even if the array doesn't contains any zeroes.. The base cases are also so simple to handle over here.
@anshugangwar7344
@anshugangwar7344 2 жыл бұрын
Calculating Max Sum of Array with the following condition, If A[i] element contributing in max sum,than you can't consider A[i]-1 and A[i]+1 element for max sum contribution, if present in array . Note: Elements of the array can be repeated multiple times. It's unsorted. It can have 1 to n number of element. eg: int A[ ]={1,6,7,1,2,3,3,4,5,5,5,6,9,10}; If I consider 10 as contributing max sum, I can consider 9 and 11. (Here 11 is not present but 9 is present still we can’t consider it). If I consider 5 (total max sum contribution 5*3) but can’t consider 4 and 6. I have tried sorting and storing index and count in the ordered map. Also try to compare with unbounded knapsack criteria. Still, I was struggling with an optimal and clear idea to calculate. Any idea how to approach it .
@user-nz1nq2el4r
@user-nz1nq2el4r Жыл бұрын
Create an array dp of size n, where n is the length of the input array A. Initialize all elements of dp to 0. Sort the input array A in ascending order to simplify the comparison process. Iterate through each element A[i] in the sorted array A. For each element A[i], update the maximum sum at index i in the dp array based on the following condition: If A[i] is the first occurrence in the sorted array or A[i-1] and A[i+1] are not present in the sorted array, then set dp[i] to dp[i-1] + A[i]. Otherwise, set dp[i] to the maximum value between dp[i-1] (excluding A[i]) and dp[i-2] + A[i] (including A[i]). After iterating through all elements in the sorted array, the maximum sum will be the maximum value in the dp array.
@studynewthings1727
@studynewthings1727 3 ай бұрын
understood😃
@per.seus._
@per.seus._ 20 күн бұрын
understoooooooood
@nehakaushik3602
@nehakaushik3602 2 жыл бұрын
OMG bhaiya ...simple "genius"
@sujalgupta6100
@sujalgupta6100 Жыл бұрын
Understood. Best on whole yt.
@itsmepratham2712
@itsmepratham2712 Жыл бұрын
For Those who did not understand why total sum-d has to be even so imagine totalsum=15 and d=2 now count s2=(15-2)/2 it will give s2=6; ans s1-s2=d so s1=2+6=8 now here it is said that s1+s2=totalsum but s1+s2=14 which is not equal to total sum to total sum-d has to be even
@molymusic1188
@molymusic1188 Ай бұрын
thankyou my guy🙌
@SethuIyer95
@SethuIyer95 4 ай бұрын
Understood.
@rahulprasad3575
@rahulprasad3575 Жыл бұрын
what we do in case of negative elements if its given?
@dawarvaibhav
@dawarvaibhav 2 жыл бұрын
Love it ✌🏻☺️
@learnandteach3527
@learnandteach3527 5 ай бұрын
amazing
@harisrashid0773
@harisrashid0773 Жыл бұрын
int gfg the case where 0 can be in the begining can be solved by sorting given array in decreasing order but this will increase time compl,but will still pass test cases;
@stain0p101
@stain0p101 9 ай бұрын
Thank you striver, made it super easy and interesting to learn DP. "UNDERSTOOD" 💓💓
@swagcoder
@swagcoder 10 ай бұрын
Can anyone please explain why target is starting from 0 here? in all other problems it's starting from 1. If I take 1, some test cases are failing
@akr1831
@akr1831 2 жыл бұрын
Thankyou very much for explaining base case of last question. I was stacked for last 6 hours. ❤️❤️
@aryanagrawal4794
@aryanagrawal4794 2 жыл бұрын
bhaiya in count subset problem which u had taken earlier , in tabulation the loop started from 0 to sum but when sum==0 it has been handled previously right? then it should start from 1 right? as in problem subset sum==k u started the sum loop from 1 to sum as for sum==0 we have handled it previously before the loop.
@takeUforward
@takeUforward 2 жыл бұрын
Yes u can, won’t be an issue :)
@aryanagrawal4794
@aryanagrawal4794 2 жыл бұрын
@@takeUforward yes i did that in the count subsets problem, but when I did that in the current problem of partition i.e. looping from 1 to sum it's giving WA, but when i changed to 0 to sum it's giving correct and. Why so?
@takeUforward
@takeUforward 2 жыл бұрын
@@aryanagrawal4794 because in partition at 0 there are other cases to handle. Here at 0, its take and notTake, hence
@aryanagrawal4794
@aryanagrawal4794 2 жыл бұрын
@@takeUforward ok bhaiya got it.
@madhurnarang2440
@madhurnarang2440 Жыл бұрын
@@aryanagrawal4794 Hey can you explain it to me?
@andrewcenteno3462
@andrewcenteno3462 Ай бұрын
That algebra at 10:15 is crazy I would never see that under the time constraints of a real interview. It's brilliant tho
@sanjitvyas3043
@sanjitvyas3043 9 ай бұрын
Understood
@sameersahu4569
@sameersahu4569 2 жыл бұрын
Understood!!Thank you
@SatyamKumar-bw4vi
@SatyamKumar-bw4vi 8 ай бұрын
Hare Krishna..!! understood.
@ayushsolanki6915
@ayushsolanki6915 2 жыл бұрын
Can someone please explain me the case if(nums[0]
@tallalhabib4526
@tallalhabib4526 Жыл бұрын
What if we find with target = totalsum Then at n-1 iteration of tabulation find S1 and S2 by totalsum - S1. Then If S1>=S2 and S1-S2=D we can return True else False
@vaibhavdeshmukh7900
@vaibhavdeshmukh7900 Жыл бұрын
Below code is failing on 7th test case, giving wrong answer. Can someone help in finding what is the mistake I am making. int countParts(vector &arr, int sz, int d, int index, int s1, int s2, vector &dp){ if(index == -1){ if(s1>=s2 and s1-s2 == d) return 1; return 0; } int mod = 10e9 + 7; if(dp[index][s1] != -1) return dp[index][s1]; int pick = 0, notPick = 0; if(s1>=s2) pick = countParts(arr, sz, d, index-1, s1-arr[index], s2+arr[index], dp); if(s1>=s2)notPick = countParts(arr, sz, d, index-1, s1, s2, dp); return dp[index][s1] = (pick%mod + notPick%mod)%mod; } int countPartitions(int n, int d, vector &arr) { int totSum = 0; for(auto &val: arr) totSum += val; vector dp(n+1, vector(totSum+1, -1)); return countParts(arr, n, d, n-1, totSum, 0, dp); }
@madmaxgaming5864
@madmaxgaming5864 11 ай бұрын
just mind blowing how you came up with the modified target, kyse kar lete ho bhaiya?
@VikasGupta-ok9lh
@VikasGupta-ok9lh Жыл бұрын
In that case my recursive code was going from 0 to n-1 and to avoid case of zero I sorted my array hence all the zero cases were sorted
@inderjotsingh5868
@inderjotsingh5868 Жыл бұрын
you don' t even need this , just go from 0 -> n , and if at n , target == 0, return 1 else 0 , all other cases are already been taken care off , if you do in this way . How ? our code will do this will we are making the recursive calls at n-1
@siddharth794
@siddharth794 2 жыл бұрын
Is there any easy way to write base case as I am getting really confused
@rechinraj111
@rechinraj111 2 жыл бұрын
You have expressed S1 in terms of S2 then solved this problem. Can we do vice versa then solve this problem?
@ganeshkamath89
@ganeshkamath89 2 жыл бұрын
Thanks Striver. Understood.
@affanrahman5711
@affanrahman5711 2 жыл бұрын
In lecture 17, can we handle the cases having zeroes by just multiplying our previous answer with pow(2,n) where n is the no. of zeroes
@takeUforward
@takeUforward 2 жыл бұрын
I discussed that only, but that adds a log n
@sourabhchoudhary7289
@sourabhchoudhary7289 2 жыл бұрын
@@takeUforward Does 1ll
@uddalakseal9320
@uddalakseal9320 6 ай бұрын
Can anyone explain why are we always initializing the DP[0][i] with val[0]: for(int i = weight[0];i
@kathanvakharia
@kathanvakharia Жыл бұрын
Understood...Completed 18/56
@gauravgogoi5240
@gauravgogoi5240 3 ай бұрын
15:00 the most important point to be noted if you are in an intervie
@001_abhijeetkumar7
@001_abhijeetkumar7 2 ай бұрын
why return 0 case is not considered in base case of tabulation?
@deepanshuthakur140
@deepanshuthakur140 3 ай бұрын
new welcome line
@Anonymous-uj3jx
@Anonymous-uj3jx 2 жыл бұрын
Understood thanks :)
@AKASHKUMAR-we5hg
@AKASHKUMAR-we5hg Жыл бұрын
why line number 23 if(num[0] != 0 && num[0]
@kumarpurushottam632
@kumarpurushottam632 Жыл бұрын
Thanks a Lot Striver 🥰
@m-bk4um
@m-bk4um 8 ай бұрын
understand
@sahilarya9510
@sahilarya9510 2 жыл бұрын
please tell us the time when this amazing course will get end......expected time??
@dharsan.s7937
@dharsan.s7937 2 жыл бұрын
March
@techietech7073
@techietech7073 Жыл бұрын
for DP 17 for [ 7,1,0,2,5] with tar=7 Method 1: originalAns*(pow(2,n)) Method2: Changes in base case will that work? I can see from recursion tree there would be redundant calls at level where 0 is considered
@YatriSpecial
@YatriSpecial Жыл бұрын
How do I write the base case in tabulation, Could you please tell me?
@nithish_raina
@nithish_raina Жыл бұрын
Method 1 is time intensive if let's say the no of zeroes were 15-20 in an array...Hence method 2 with handling zero in the base cases is required at such cases.
@ayushraj3260
@ayushraj3260 8 ай бұрын
can we not put if sum == -1 then return 1;
@himanshuagrawal8012
@himanshuagrawal8012 2 жыл бұрын
as we are handing separately so now we need our TARGET LOOP should start from 0...correct ??
Clowns abuse children#Short #Officer Rabbit #angel
00:51
兔子警官
Рет қаралды 74 МЛН
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 162 МЛН
DP 20. Minimum Coins | DP on Subsequences | Infinite Supplies Pattern
34:15
VICKY KAUSHAL REACTS TO VICKY KAUSHAL MEMES ft. VICKY KAUSHAL
26:42
Tanmay Bhat
Рет қаралды 5 МЛН
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 792 М.
11 Count the number of subset with a given difference
16:51
Aditya Verma
Рет қаралды 246 М.
Clowns abuse children#Short #Officer Rabbit #angel
00:51
兔子警官
Рет қаралды 74 МЛН