Тёмный
No video :(

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

take U forward
Подписаться 651 тыс.
Просмотров 188 тыс.
50% 1

Опубликовано:

 

24 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 612   
@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 7 месяцев назад
understood
@himalayadebbarma-we4pt
@himalayadebbarma-we4pt Месяц назад
Understood
@AbhishekKumar-cv1dh
@AbhishekKumar-cv1dh 9 месяцев назад
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 3 месяца назад
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 3 месяца назад
for real should have moved on to this video those comments were so confusing
@SamreenAftab-lg8tx
@SamreenAftab-lg8tx 2 месяца назад
Samee
@solitucedagar4298
@solitucedagar4298 Месяц назад
sameeeeeeeeeeee
@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 10 месяцев назад
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 7 месяцев назад
@@entertainmenthub373 God of cp is tourist.
@divyanshrawat2859
@divyanshrawat2859 2 месяца назад
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.
@nikhilmadaan29
@nikhilmadaan29 2 месяца назад
hats off man,, spent 2 hours on DP17 to fix that but after watching this DP 18 everything went super smooth
@Aniruddha_Chowdhury
@Aniruddha_Chowdhury 3 месяца назад
understood until 14:00 ❤ . Will learn the optimization later
@PIYUSH61004
@PIYUSH61004 2 года назад
The deduction of (totalSum - D) / 2 was amazing. Understood!
@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
@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 Год назад
Best Bhai @@anuragprasad6116
@manojnandhan8677
@manojnandhan8677 29 дней назад
but how will you handel -1 index in tabulation?
@saurabhsaha6958
@saurabhsaha6958 4 месяца назад
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.
@pulkitchausali1354
@pulkitchausali1354 Год назад
Already understood before starting of video that's what Striver teaches us
@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.
@cs26divyanshisachan37
@cs26divyanshisachan37 7 месяцев назад
Understood! Hats off to ur dedication, u are still teaching while suffering from fever.
@ankanbasu7381
@ankanbasu7381 2 года назад
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
@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 10 месяцев назад
but for me it creates problem while writing the tabulation form. Esp. with all the subsequences questions.
@chase.2595
@chase.2595 10 месяцев назад
yeah man, we have to start from n-2 if we do 0 to n-1 in recursion@@santoshpokhrel7693
@ganeshktvb9234
@ganeshktvb9234 7 месяцев назад
yaa bro really it creates confusion in tabulation @@santoshpokhrel7693
@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 2 месяца назад
thankyou my guy🙌
@Lyrics_dunya
@Lyrics_dunya 6 месяцев назад
For Problem 17 , I struggled a bit in Tabulation while writing the base cases for the new changes. All 3 cases covered, also notice the bases cases are written in reverse order, so that the priority is given to the last one if its true. One more thing since we are considering the case for i = 0, please start the 2nd loop of target from 0 till k(inclusive). Happy learning! dp[0][0] = 1 if arr[0]
@TyagiAviral
@TyagiAviral 2 месяца назад
Hey, thank you. Great comment.
@ScienceSeekho
@ScienceSeekho 2 года назад
understood. Also thanks for explaining Space Optimization so good. I am from tier-3 mechanical never knew all these stuff
@narendramaurya4823
@narendramaurya4823 5 месяцев назад
Us, but the way you transformed the problem into previously solved problem is amazing , that's the way we have to think ... Thanks ❤
@chaitralishinde7815
@chaitralishinde7815 18 дней назад
Understood better than ever!
@soumik5854
@soumik5854 Месяц назад
solved this problem on my own very proud of this
@manishisaxena5657
@manishisaxena5657 Год назад
actually we can make the base case as if(index == -1) { if(tar == 0) return 1; return 0; }
@anuragprasad6116
@anuragprasad6116 Год назад
underrated! taking this as a base case really simplifies the solution.
@vaibhavrai7042
@vaibhavrai7042 24 дня назад
To deal with the 0 case, we can also take the dp array to be of size [n+1][sum+1] and initialize the dp[n][0]=1 int[][] dp = new int[n+1][sum+1]; dp[n][0]=1; for(int index=n-1;index>=0;index--)
@stith_pragya
@stith_pragya 7 месяцев назад
UNDERSTOOD.........Thank You So Much for this wonderful video...............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@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.
@user-ve4jm9pj3s
@user-ve4jm9pj3s Месяц назад
another base case(Better and Concised): if(ind
@ShahNawaz-cx3pi
@ShahNawaz-cx3pi 2 месяца назад
*********** Iterative Code for the count number subsequence whose sum is k ( & 0
@adebisisheriff159
@adebisisheriff159 7 месяцев назад
Understood! Striver. The best Software Engineer himself
@hashcodez757
@hashcodez757 24 дня назад
"UNDERSTOOD BHAIYA!!"
@parthsalat
@parthsalat 2 года назад
Understood 💯💯Great Explanation. Thank you very much for all you efforts🔥🔥
@gauravgogoi5240
@gauravgogoi5240 4 месяца назад
After this video, they updated the problem! Real influencer
@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 .👍
@aadarsh9719
@aadarsh9719 7 месяцев назад
Why code fails for GFG question?
@mohammednooruddin4165
@mohammednooruddin4165 2 месяца назад
Send ur code
@harshitsharma5647
@harshitsharma5647 Год назад
Superb Bhaiya guys watch at 10:10 Amazing Concept Again and again Thankyou bhaiya Aka Striver
@Parthj426
@Parthj426 Месяц назад
us But i got confused as in DP 18 ques , it is written that two partitions have their union as Whole array . It should have been given that both are exclusive of each other , for being self-explanatory well.
@the_shridhar
@the_shridhar 3 месяца назад
just add this in previous question: ``` if (i < 0) { return !target; } ```
@shriRadheHariom
@shriRadheHariom Месяц назад
Understood, well explained.
@shubh625
@shubh625 Месяц назад
ok
@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 2 года назад
@@aryanagrawal4794 Hey can you explain it to me?
@UECAshutoshKumar
@UECAshutoshKumar 2 месяца назад
Understood!
@user-zf6cz2th3s
@user-zf6cz2th3s 7 месяцев назад
understood
@saileshsirari2014
@saileshsirari2014 17 дней назад
Other was is just check when you reach end of nums int rec (int pos, int [] nums , int target,int sum, int mem [] []){ if(pos == nums.length){ if(target==0 ) return 1; return 0; } if( mem[pos][target]!=-1) return mem[pos][target]; int take =0; if(target>=nums[pos]){ take = rec(pos+1,nums,target- nums[pos],sum,mem); } int nonTake = rec(pos+1,nums,target,sum,mem); mem[pos][target] = nonTake+take; return mem[pos][target]; }
@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
@ritikshandilya7075
@ritikshandilya7075 2 месяца назад
Great Explanation Striver , Thanks
@MukeshKumar-cc3uh
@MukeshKumar-cc3uh 5 месяцев назад
Understood 👍. Hats off to your dedication for us. ♥
@andrewcenteno3462
@andrewcenteno3462 2 месяца назад
That algebra at 10:15 is crazy I would never see that under the time constraints of a real interview. It's brilliant tho
@gauravgogoi5240
@gauravgogoi5240 4 месяца назад
15:00 the most important point to be noted if you are in an intervie
@akr1831
@akr1831 2 года назад
Thankyou very much for explaining base case of last question. I was stacked for last 6 hours. ❤️❤️
@techietech7073
@techietech7073 2 года назад
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.
@sahilarya9510
@sahilarya9510 2 года назад
please tell us the time when this amazing course will get end......expected time??
@dharsan.s7937
@dharsan.s7937 2 года назад
March
@anveshkarra3861
@anveshkarra3861 2 года назад
understood , the space optimization is amazing sir
@RVcalisthenics
@RVcalisthenics 3 дня назад
understood sir🙂
@parthsalat
@parthsalat 2 года назад
Guys! For DP 17, Memoization code, striver's approach is correct (6:38), but here's a cleaner alternative for the base case: if(ind
@abhishekgururani6993
@abhishekgururani6993 2 года назад
It'll be hard to convert this base case to tabulation dp states as it's not well defined wrt the indices.
@parthsalat
@parthsalat 2 года назад
@@abhishekgururani6993 You're right. But the tabulation base cases are straightforward and simple. One can write that by applying basic logic.
@amitmahato6404
@amitmahato6404 2 года назад
Yes, I also tried this. got correct in both tabulation and memoization. Striver has complicated this solution a little bit.😅
@amitmahato6404
@amitmahato6404 2 года назад
@@abhishekgururani6993 Simple base case, if sum == 0, return 1; means fill all the 0 indices row with 1. The change is that you have to traverse j=0 to j
@priyankabagal9269
@priyankabagal9269 2 года назад
You have literally made dp look so easy !
@shubhrajyotipoddar1684
@shubhrajyotipoddar1684 4 месяца назад
if(k==0) dp[ind][k]= 1; if(ind==0) return (arr[ind]== k)?1:0; I feel like not returning and instead adding 1 to the dp should do the trick And in case of tabulation: dp[0][0] = 1; this should do instead of the for loop of ind= 0 to n-1{dp[i][0] =1}
@shiershdwivedi742
@shiershdwivedi742 2 года назад
"Hi Striver, This is Thriver here hope you are doing extremely well "😅
@tasneemayham974
@tasneemayham974 11 месяцев назад
UNDERSTOOODD!!! Thank you, Striver!!
@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
@shoryapawar2219
@shoryapawar2219 11 месяцев назад
Understood Thanks Striver for this Dp series
@sonicboom6635
@sonicboom6635 9 месяцев назад
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?
@fanofabdevillersandmathslo5960
@fanofabdevillersandmathslo5960 5 дней назад
Understood
@akashkumarsingh3595
@akashkumarsingh3595 2 года назад
If we have handled the case num[0] == 0 previously then why we are checking it again for num[0]
@sushrutasengupta1273
@sushrutasengupta1273 3 месяца назад
If we had added the equations then s=T+D/2 would've happened which would remove requirements of ec 1
@tami9154
@tami9154 Год назад
wouldn't it be better to go an ind
@shreyashtech8556
@shreyashtech8556 5 месяцев назад
bro doing gods work
@nehakaushik3602
@nehakaushik3602 2 года назад
OMG bhaiya ...simple "genius"
@musichub2168
@musichub2168 2 года назад
Understood 💯💯Great Explanation
@LBK3
@LBK3 Год назад
understood ❤
@user-is6ky7pp2n
@user-is6ky7pp2n 8 дней назад
understood !!
@kathanvakharia
@kathanvakharia Год назад
Understood...Completed 18/56
@sanskarjaiswal4394
@sanskarjaiswal4394 9 месяцев назад
Instead of changing the code we can just add a if statement before "take" variable that if arr[index]== 0 then skip it...
@prabhakaran5542
@prabhakaran5542 5 месяцев назад
Understood ❤
@deepanshuthakur140
@deepanshuthakur140 4 месяца назад
new welcome line
@khushigupta5798
@khushigupta5798 6 месяцев назад
UNDERSTOOD
@LowkeyCoder
@LowkeyCoder 9 месяцев назад
"Understood"!
@ranasauravsingh
@ranasauravsingh 2 года назад
UNDERSTOOD... ! Thanks striver for the video... :)
@DevashishJose
@DevashishJose 8 месяцев назад
understood. Thank you so much
@ramyasree4986
@ramyasree4986 2 года назад
completed subsequences set problems great learning
@hrushikesh-1914
@hrushikesh-1914 11 месяцев назад
Understood. Thankyou sir.
@aps8874
@aps8874 Месяц назад
Thank you so much!
@atanunayak6637
@atanunayak6637 2 года назад
Respect ❤
@AshishYadav-ql3up
@AshishYadav-ql3up 28 дней назад
loved it
@sakshamsengar9798
@sakshamsengar9798 2 года назад
if we keep base condition like this then we dont have to check for individual case ....this is giving right answer..... if(i==n){ if(s1==0)return 1; return 0; } if(s1
@madmaxgaming5864
@madmaxgaming5864 Год назад
just mind blowing how you came up with the modified target, kyse kar lete ho bhaiya?
@saikartik2202
@saikartik2202 Месяц назад
or I think we can remove all the base cases and simply put this for DP-17: if(ind
@ankishkhandelwal1061
@ankishkhandelwal1061 2 года назад
In case arr =1,2,3,4 Diff =2 S1>S2 Target is =(10-2)/2 =4 Give 2 as answer 4 3,1 But 1+2+3-4=diff How this case will handle
@ShahNawaz-cx3pi
@ShahNawaz-cx3pi 2 года назад
In question it is mentioned that the union of both the set is equal to the given array.
@ankishkhandelwal1061
@ankishkhandelwal1061 2 года назад
@@ShahNawaz-cx3pi got it
@mayankmalhotra21
@mayankmalhotra21 Год назад
if someone is going from 0 to n if(i==arr.length) { if(tar==0) return 1; else return 0; } this will handle the case for zero (1, 0,0 ) will give -> 4 (if tar = 1)
@SatyamKumar-bw4vi
@SatyamKumar-bw4vi 9 месяцев назад
Hare Krishna..!! understood.
@HarshJha-k4q
@HarshJha-k4q 2 месяца назад
also on ind==0 if(arr[ind]==sum) return 1;
@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
@swagcoder
@swagcoder 11 месяцев назад
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
@drishtirai864
@drishtirai864 5 месяцев назад
Understood !!
@abhisheks7450
@abhisheks7450 22 дня назад
why doesnt the for(int i = 0;i
@per.seus._
@per.seus._ Месяц назад
understoooooooood
@TON-108
@TON-108 10 месяцев назад
Understood Bhaiya!
@kushjoshi9716
@kushjoshi9716 Год назад
Nicely Explained! Understood!
@jk-sm6qr
@jk-sm6qr 2 месяца назад
Thank you
@harisrashid0773
@harisrashid0773 2 года назад
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;
@sujalgupta6100
@sujalgupta6100 Год назад
Understood. Best on whole yt.
@parthsalat
@parthsalat 2 года назад
DP 18 starts from 7:25
@abhijeetbasfore6816
@abhijeetbasfore6816 2 года назад
Thank you so much I've 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?
Далее
Yana bir yangi qo'shiq YORAM BIYO | Yaqin kunlarda
00:57
would you eat this? #shorts
00:35
Просмотров 1,8 млн
DP 17. Counts Subsets with Sum K | Dp on Subsequences
36:57
10 Minimum Subset Sum Difference
46:41
Просмотров 381 тыс.
DP 15. Partition Equal Subset Sum | DP on Subsequences
9:43