L11. Subset Sum II | Leetcode | Recursion

  Рет қаралды 327,125

take U forward

take U forward

Күн бұрын

Check our Website:
In case you are thinking to buy courses, please check below:
Link to get 20% additional Discount at Coding Ninjas: bit.ly/3wE5aHx
Code "takeuforward" for 15% off at GFG: practice.geeks...
Code "takeuforward" for 20% off on sys-design: get.interviewr...?_aff=takeuforward
Crypto, I use the Wazirx app: wazirx.com/inv...
Take 750 rs free Amazon Stock from me: indmoney.oneli...
Earn 100 rs by making a Grow Account for investing: app.groww.in/v...
Linkedin/Instagram/Telegram: linktr.ee/take...
---------------------------------------------------------------------------------------------------------------------------------------------------- I have decided to make a free placement series comprising of video lectures(C++ and Java) on the entire SDE sheet.. ✅(bit.ly/takeUfo...)
✅Entire Series: bit.ly/placemen...
✅Problem Link: leetcode.com/p...
C++/Java Code: takeuforward.o...
Want to become an advanced level programmer in a structured way? Join the "Ascend" batch starting on 4th March and begin your journey to the advanced level.
Intermediate to Expert Level Coder in 10 Months (Batch preview available): unacademy.com/...
Use Code "TAKEUFORWARD" to unlock all the free classes and 10% discount on the Subscription
Get access to all the free lectures: unacademy.cc/d...
✅This is where I teach: ----- Use coupon-code "TAKEUFORWARD" for getting 10% on my course at GFG: bit.ly/striver_...
✅Use coupon-code "TAKEUFORWARD" for getting 10% for all GFG courses: bit.ly/tuf_gfgC...
If you appreciate the channel's work, you can join the family: bit.ly/joinFamily
Thumbnail Creator: / rikonakhuli
✅ Striver's Linkedin Profile: / rajarvp
✅ Instagram: / striver_79
Connect with us: t.me/Competiti... (Use Link in Mobile only, if not works search "takeUforward" in telegram)..
#dsa #leetcode #placements

Пікірлер: 378
@Vishalraj_1
@Vishalraj_1 3 жыл бұрын
most of the time, I have solved questions by myself. But then also I watch your videos, Two reasons behind this is most of the time, I get to know more approaches to solve the question. and the second thing is how I can tell the solution to the interviewer. Yesterday was my interview and I tell brute then optimal approach with intuition(like you). And the interviewer was happy with my presentation. Thanks you making videos, it help me in both problem solving + presentation skills.
@akmansr7149
@akmansr7149 2 жыл бұрын
Hi, I am still unable to solve problems on my own. what should I do? 😥😥
@omprakashhardaha7736
@omprakashhardaha7736 2 жыл бұрын
@@akmansr7149 practice
@akmansr7149
@akmansr7149 2 жыл бұрын
@@omprakashhardaha7736 bhai doing that since a very long time. Seems like i suck. I can't solve problems without looking at discussion section or yt videos 😭
@omprakashhardaha7736
@omprakashhardaha7736 2 жыл бұрын
@@akmansr7149 bro i hv also same problem I suggest follow DSA kunal kushwaha course. You can start from recursion or binary search video.
@shivansh2870
@shivansh2870 2 жыл бұрын
@@akmansr7149same problem
@CSBBADRIGAUTAM
@CSBBADRIGAUTAM Жыл бұрын
A huge thanks to you bro. This question helped get into job In my technical interview, I was to rate my coding skills and I said 8. He was supposed to ask the question of finding subsets of fixed size, and since I said my rating would be 8, he changed question to finding all subsets. I explained the code and everything. He was not believing my algo was correct. I guess What my interviewer was thinking to modify the original algo for prev question or something. He was saying I was wrong and I got worried a lot.😟😟 Then I shared my screen and drew the tree and explained everything as you did in this video (not as good as you though 😅😅) and he was *completely convinced* . He told me to move onto another question and he was still doubtful on the 1st question and did some research while I am doing my 2nd question (which is different question but same recursion application) and told me the solution for the first was the best optimal approach and got impressed a lot. 😂😂 A huge thanks to you bro.
@abc-ym4zs
@abc-ym4zs Жыл бұрын
in which software u have explained tree to the interviewer can u please tell if I encounter these type of persons it will be useful
@CSBBADRIGAUTAM
@CSBBADRIGAUTAM Жыл бұрын
@@abc-ym4zs I didn't use any software, I just opened a document and explained using "/" and "\" as the links, wrting in next lines for children nodes or slashes (/ and \) and numbers in place of nodes. I used my mouse to show how each recursion goes for. It was online interview
@akashsingh2722
@akashsingh2722 Жыл бұрын
Damn!! congrats bhai
@nimeshpareek953
@nimeshpareek953 6 ай бұрын
I was also asked this question once in an interview and I told the interviewer that we can solve this using recursion then he changed the question 😂😂
@bruvhellnah
@bruvhellnah 4 ай бұрын
​@@nimeshpareek953 what was the point of changing the question ? Are you not supposed to know the answer to what they ask lol Also what was the new question
@adityarallapalli8308
@adityarallapalli8308 Жыл бұрын
i != idx, instead of above condition, we can also write i > idx, which is intuitive.
@HarshilAndhariya
@HarshilAndhariya 4 ай бұрын
agree 🤝
@-EE-AKASHJHA
@-EE-AKASHJHA 2 жыл бұрын
I have just solved 5 medium questions of leetcode having 10000+ likes in such a way that they don't feel even like easy problems, and when I used to see those questions earlier, I got fear and a question about whether I will be able to solve these questions ever but now I have not only solved them but understood the logic as well as written the pseudo code and also drawn the recursion tree. THANK YOU SO MUCH RAJ BHAIYA...
@diyapathak2413
@diyapathak2413 Жыл бұрын
pls tell me how b/c i feel so scared looking at them rn
@RishabhSingh-xn3xu
@RishabhSingh-xn3xu Жыл бұрын
Everyone is Pro Until the new Question drops in the interview
@takeUforward
@takeUforward 3 жыл бұрын
Please leave a small comment if you understand by spending your time here, every comment motivates me more :) C++ Code: github.com/striver79/SDESheet/blob/main/Subset-II_Cpp Java Code: github.com/striver79/SDESheet/blob/main/Subset-II_Java Instagram(For live sessions): instagram.com/striver_79/​
@yuvrajluhach5665
@yuvrajluhach5665 2 жыл бұрын
For Java code github link you have given C++ code , must be a mistake . 👍 Loving this series . I'm a 3rd year student any words of wisdom?😀 Like what should I not neglect whatever happens.
@sheturaj7437
@sheturaj7437 2 жыл бұрын
One thing I did not understand is that aren't we generating wrong subsets if we are sorting the input array? For example:- if arr = [2,3,1,3,2], which after sorting would become [1,2,2,3,3], then won't [1,3,3] (and many more) which would be generated by your logic would be wrong since [3,1,3] would be the right subset?
@codingwithsmallsteps2878
@codingwithsmallsteps2878 3 жыл бұрын
Hi Striver. Thank you for the wonderful explanation of the Subsets II leetcode problem. I read the codes in discuss part for the problem but was unable to understand the logic behind it. But after watching your video, I have understood the logic and can write code for it.
@anshumanpanigrahi7817
@anshumanpanigrahi7817 3 жыл бұрын
I was having a doubt in this question, and I searched it on KZbin. Surprisingly, you solved this question before and I was delighted to know it from you.
@anmolagarwal5600
@anmolagarwal5600 2 жыл бұрын
Alternative: when ignoring current element, also ignore all elements similar to current element. void help(vector& nums, int i, vector& s, vector temp){ if(i == nums.size()){ s.push_back(temp); return; } int j = i; while(j
@aakritisingh399
@aakritisingh399 2 жыл бұрын
Indeed a great explanation, this problem has similarities to the combination sum-II problem. Enjoying the recursion series.
@jeremysilwoman
@jeremysilwoman 3 жыл бұрын
another easy way i feel is using bits. say we have one 1, two 2 and one 3. So we can choose either one or zero 1s, two or one or zero 2s and one or zero 3s. so we can have an array to store count of each element. we start with {}. now we can either use one 1s or zero 1s. So {}, {1}. Now we can use zero/one/two 2s. So we have 2, 22, 222, 12, 122, 1222, 1, {}.similarly for next element.
@shiriii-x9h
@shiriii-x9h 26 күн бұрын
I never understood at which step to call recursion function and how exactly it gets implemented. But by watching your videos concepts goes so clear to mind, I gained a lot of confidence in those topics that I watched from your channel. Thanks for making great content.
@Munchen888
@Munchen888 2 ай бұрын
24:55 Can we use a condition “if i > idx …” It would be the same as in previous videos. Thus we don’t take the element we had seen.
@rishabhkukreja6910
@rishabhkukreja6910 3 жыл бұрын
Thanks a lot for the videos You are doing a great job Sometimes even if i have done the question before i still watch your video and i learn new ways to approach the same problem Great work !! Keep going !
@Shivam-Varshney
@Shivam-Varshney 9 ай бұрын
7:29.5 is important for understanding the indexing of the f(5,{3}) they took 5 as you have moved from 4th to the 5th index of this stuff .
@pulkitjain5159
@pulkitjain5159 2 жыл бұрын
following this series made so easy to do these problems of Subset1 and SubsetII as these follow the same concept of combination1 and Combination2 ,it is improving very much concepts of Recursion ,thanks for the awesome playlist sir
@TheNishant30
@TheNishant30 2 жыл бұрын
this can also be solved with a modified take/not-take approach. it's just that when you are not taking an element, you dont only skip the current element, but also skip all its duplicates as well. essentially, you modify the recursive case to look for the next unique element. here's a pseudocode //BASE CASE if index > nums.length => push temp array's copy to result array => return //RECURSIVE CASE //all subsets that'll contain an element at a particular index => push nums[index] to the temp array =>call the function on (index+1) //all subsets that will NOT have an element at that particular index AND WILL NOT HAVE ITS DUPLICATES EITHER => pop the last element off your temp array => look for the next UNIQUE ELEMENT and call the function on that unique element's index. // here you dont only skip the element at index, but also index+1 if it's a duplicate, index+2 if it's also a duplicate..... so on and so forth, until you find the next unique element in the array. once you do, you call the function on that index. but what happens when there's no next unique element [1, 2, 2]? when you exclude 2 and look for the next UNIQUE element, will you find it? NO. what will you do then? not make a recursive call AT ALL? how will you reach the leaf node then? the base condition for that branch will not get satisfied and you will skip combinations that should have been there. HINT: IF THERE IS NO "NEXT" UNIQUE ELEMENT, CALL THE FUNCTION ON THE LAST DUPLICATE. let i = index+1 for(i; i < nums.length; i++) //Javascript folks, just use a var declaration here (-_-) { if (nums[i] == nums[index]){ continue; } else { break; } } => call the function and pass i as index for that call.
@Shivam-Varshney
@Shivam-Varshney 8 ай бұрын
25:02 if I!=Ind && nums[I] == num[i-1] continue and don't pick these conditions It means your I should be equal to index to be picked up in the call and if 2 successive indexes of array are same then don't pick up them in recursion call 🤙😎
@aratrik247
@aratrik247 2 жыл бұрын
Watched ur previous videos on recursion and coded this one by myself....I am amazed I can finally solve recursion problems.... Thanks bhaiya for ur wonderful explanations😃
@vedangfate6290
@vedangfate6290 Жыл бұрын
Same here bro. The way he has simplified the whole process is so amazing
@AdityaKumar-be7hx
@AdityaKumar-be7hx Жыл бұрын
understood! The problem as well as the fact that I don't need to follow any one else for my algorithmic preparation. Keep up the great work!
@anishkumargiri9490
@anishkumargiri9490 Жыл бұрын
Efficient solution said by you is having the same time complexity as of brute force becuase in brute force it is going to take O(2^nlog2^n) and logbase2 to 2^n will be n so 2^n*n so what is the difference in time complexity between brute and efficient they are same.
@yuvrajluhach5665
@yuvrajluhach5665 2 жыл бұрын
Combination II helped with this one , coded solution without referring video solution👍 developing the intuition
@ashutoshthite
@ashutoshthite 2 жыл бұрын
that's true !
@atharvakalele37
@atharvakalele37 2 жыл бұрын
the best channel for competitive programming! thanks a lot brother!
@rohitsai9010
@rohitsai9010 2 жыл бұрын
Fantastic explanation.. U made recursion more easy and interesting.. Hats off your to patience
@iWontFakeIt
@iWontFakeIt 2 жыл бұрын
ooooh! man, i solved this problem at first but i was wondering what went wrong as this problem is similar to combination sum problem, but finally i realised that i need to add all subsets into the final array. Hence i solved the problem and successfully understood the subtle difference between these two problems.
@av21015
@av21015 2 жыл бұрын
Yeah me too faced the same issue . I didn't think there is no base case required.
@shreyxnsh.14
@shreyxnsh.14 4 ай бұрын
@@av21015 yeah, i was returning for the base case, thats why it wasnt working
@aishwaryas8283
@aishwaryas8283 3 ай бұрын
Thanks for the awesome explanation Striver. Can anyone help me understand why we are not using the pick and not-pick approach for the combination sum2 and subset sum2 problems? Didnt completely understand the reason for the differences in the approaches chosen.
@neerajkumar-zb3jm
@neerajkumar-zb3jm Жыл бұрын
Very Very Good Explanation , Very Helpful , keep on making these kind of videos.
@tharungr7701
@tharungr7701 4 ай бұрын
simple as that, if array does not contains duplicates use pick and non pick method , if it has duplicate use for loop method :)
@Lalit_Shirsath
@Lalit_Shirsath 3 жыл бұрын
for reducing time complexity ....can we use unordered set ? because all operations like insert will be O(1) ........ can we do that
@joydeb8202
@joydeb8202 3 жыл бұрын
Thanks a lot bhaiya... Abhi apka CP sheet kar raha... SDE Sheet iske baad🙌 Sukriya for all this help.❤️❤️
@tanmaysatsangi131
@tanmaysatsangi131 2 жыл бұрын
plz anyone explain why the complexity of O(nlogn) to inset into set. OR I missed something at 2:22. Thanks in advance
@parthsalat
@parthsalat 2 жыл бұрын
C++ code starts at 25:30
@rishikas8276
@rishikas8276 2 жыл бұрын
I was trying to solve questions but for topics like recursion ,backtracking , graphs and stuff I’m not able to arrive at the solutions by myself ..I’m ending up referring to the solutions for most of questions like combinations , printing permutations etc Can you please tell me how to go about it so that I can come up with the solutions myself or if I’m doing something wrong Thanks!
@kunal4710
@kunal4710 Жыл бұрын
ALWAYS TRY TO DRAW RECURSION TREE OF WHATEVER CODE COMES TO YOUR MIND THEN ONLY YOU WILL GET TO KNOW WHERE YOU ARE WRONG
@dennyage4791
@dennyage4791 2 жыл бұрын
but how' s the time complexity of converting hashset to List is ml* logm ?
@CrazyHunk14
@CrazyHunk14 5 ай бұрын
able to solve this without seeing ur video!! cheers to your teaching method
@hadiali5922
@hadiali5922 2 жыл бұрын
We can even do it using find possible sub-arrays right? We can take a Hashset and we can simply store all the subarrays which are generated and HashSet will only take those subarrays which are unique in nature. Feel free to correct my approach .
@yashjain93
@yashjain93 2 жыл бұрын
Set will take NLogN time to run n elements
@apoorvgupta1211
@apoorvgupta1211 4 ай бұрын
Hey Striver, Why do you use For loop in some solutions while not in others? When do we go for the For loop. I understand that this is just reducing effort of picking the element one at a time. Please let me know if i am wrong.
@bhaveshkumar6842
@bhaveshkumar6842 2 жыл бұрын
This is the best placement series!!!!
@AlbertoRodriguez-oe6jo
@AlbertoRodriguez-oe6jo 3 жыл бұрын
This is a backtracking problem, I mean recursion + backtracking, I thought I was missing something in the explanation but recognized it when I looked at the code.
@GauravSharma-ij1ym
@GauravSharma-ij1ym 7 ай бұрын
Bruteforce approach TC should be 2^n * k*Logm where k is average sizE of subset and m(~2^n) is no. Of elements in the set . Why did u take 2^n * m*Logm. Subset size will not be 2^n. Can anyone explain?
@aaryankedia3919
@aaryankedia3919 Жыл бұрын
Hey! Thank you such a clear explanation. I had a doubt, for the time complexity(TC), wont we add the time required for sorting the list?
@rohandevaki4349
@rohandevaki4349 Жыл бұрын
pick and not pick is working with arralist contains method ,its time complexity is O(K), so total time complexity is (2*N *k),where k is the number of combinations
@kunal4710
@kunal4710 Жыл бұрын
//NOT OPTIMAL APPROACH AS WE ARE USING EXTRA SPACE AS SET FOR STORING ELEMENTS void solve(int i,vector& nums,vector temp,set< vector > &s){ if(i==nums.size()){ sort(temp.begin(),temp.end()); s.insert(temp); return; } //include temp.push_back(nums[i]); solve(i+1,nums,temp,s); temp.pop_back(); //Exclude solve(i+1,nums,temp,s); } vector subsetsWithDup(vector& nums) { vector ans; set< vector > s; vector temp; solve(0,nums,temp,s); for(auto it=s.begin();it!=s.end();it++){ ans.push_back(* it); } return ans; } SEE IT WORKS BUT IN THIS CASE YOU HAVE TO USE EXTRA SPACE HENCE NOT OPTIMAL
@neilmaheshwari9600
@neilmaheshwari9600 3 жыл бұрын
Striver(bhaiya) i can't say aapki videos kitni helpful hai bs itna kahunga maine 5 6 alag alag logon ke recursion videos dekhe kabhi concept itna clear ni hua jitna aapse hua hai its a humble request aise hi guide karte rahiye and DP ki bhi series nikal dijiye...
@wisdomkhan
@wisdomkhan 2 жыл бұрын
this question is less of recursion but more in optimising the input and output
@vaibhavsingh1282
@vaibhavsingh1282 Жыл бұрын
asking from the people who have cracked these interviews do u actually tell the interviewer the brute force and optimal approach of every problem or we can just tell them one approch
@amitghosh4548
@amitghosh4548 3 жыл бұрын
I didn't understand the space complexity. Why it will take extra O(K) [k is average length] won't it take O(2^n) + O(K) ? I guess a single ds is used to store and remove subsets.... Please someone explain if I m wrong 🤐
@abhimanyu6534
@abhimanyu6534 3 жыл бұрын
Great explanation But I think SDE sheet is getting difficult now 🥺😬😬
@tekken1935
@tekken1935 3 жыл бұрын
Did u finish?. How is placement going?
@abhayj8706
@abhayj8706 3 жыл бұрын
@@tekken1935 i am in first year I am on dp
@techbucks7339
@techbucks7339 Жыл бұрын
Been 2 years how you doing
@rohandevaki4349
@rohandevaki4349 Жыл бұрын
even the powerset method with bit manipulation without recursion works here, which time complexity is 2^n * n
@praveenkumarshah8581
@praveenkumarshah8581 3 жыл бұрын
please use light color ink on dark background...
@SDE-wq4tl
@SDE-wq4tl 2 жыл бұрын
how the power set of [4,4,4,1,4] will include set [1,4,4] and they did not ask to sort the array and then get power set?
@akshitmangotra5370
@akshitmangotra5370 2 жыл бұрын
Thanks man! You make it so so so easy!
@yashreddy7554
@yashreddy7554 6 ай бұрын
Can anyone help why there is no base case for the recursion
@reema7752
@reema7752 4 ай бұрын
Our base case would have been: if index == len(arr): return but here, there is a for loop in the code that only goes till the last index, so index wont go beyond the length of the array, so no need for that base case
@SatyamKumar-bw4vi
@SatyamKumar-bw4vi 2 жыл бұрын
Hare Krishna! Great!
@pardhi8959
@pardhi8959 7 ай бұрын
hi man, thankyou for making video for us even if your not able to speak loud after tumor operation. ❤ love you bhaiya
@anuragdas7608
@anuragdas7608 Жыл бұрын
hey Striver why didn't you did i>ind similar to the one you did in combination sum II video as they are quite similar in terms of conditions having unique solution sets ? Also the question states that return the solution in any order so it doesn't matter if you sort the array or not as it would increase the time complexity :)
@kunal4710
@kunal4710 Жыл бұрын
IF U WANT U CAN DO i>ind IT WILL BE SAME AS WE HAVE TO MAKE SURE THAT WE PICK 1ST ELEMENT AND AFTER THAT WE CAN SKIP ELEMENTS ALSO IF U WANT TO SKIP DUPLICATES WITHOUT TAKING SET YOU HAVE TO SORT IT THEN ONLY DUPLICATE ELEMENTS WIIL BE TOGETHER AND YOU CAN SKIP IT BY COMPARING IT TO PREVIOUS NUMBER
@AtulKumar-c4x7l
@AtulKumar-c4x7l Жыл бұрын
understood Thank you striver for such an amazing explanation..
@ranasauravsingh
@ranasauravsingh 2 жыл бұрын
UNDERSTOOD... !!! Thanks striver for the video... :)
@DIVYANSHMISHRA08
@DIVYANSHMISHRA08 3 жыл бұрын
from index we have to search (all combination)till last element hence func(i+1,...) in this instead of index+1?
@takeUforward
@takeUforward 3 жыл бұрын
You picking i, so i+1
@_aka5h
@_aka5h 2 жыл бұрын
Isn't the time complexity O( n * 2^n ) since we are pushing a vector every time recursive function is called.
@parthsalat
@parthsalat 2 жыл бұрын
Complexity analysis at 20:30
@hh8xking30
@hh8xking30 3 жыл бұрын
Hi Striver, if we are not returning from recursive call then, all those recursive call will be stored only in memory. So, should not we had to return them ...?
@curiossoul
@curiossoul Жыл бұрын
recursive call either return or end up filling the call stack and result in stack overflow. You can imagine recursive stack as normal Stack data structure with finite memory slots. If terminating condition is not added, it will blow up.
@saketmehta6697
@saketmehta6697 2 жыл бұрын
Will Interviewer be happy with that extra sort at starting? bcz that's again going to add nlogn time to our TC!
@Metalrave
@Metalrave 2 жыл бұрын
an additional time of NlogN (due to sorting) would be very small (almost negligible) to the recusion's 2^n i.e exponential complexity.
@Rajat_maurya
@Rajat_maurya 2 жыл бұрын
void f(vector &v1,vector &ans,vector &ds,int i) { ans.push_back(ds); for(int j=i;j
@s.hariharanreddy5439
@s.hariharanreddy5439 2 жыл бұрын
same problem here.. did you got the solution?
@s.hariharanreddy5439
@s.hariharanreddy5439 2 жыл бұрын
actually that should j+1 instead of i+1
@Rajat_maurya
@Rajat_maurya 2 жыл бұрын
@@s.hariharanreddy5439 yes you are correct
@thecreator7963
@thecreator7963 2 жыл бұрын
@@s.hariharanreddy5439 thanks it's helps me a lot
@ark178
@ark178 2 ай бұрын
How is this different from combination sum 1? why we need a loop here and not in subset sum1?
@sohamtanpathak9067
@sohamtanpathak9067 Жыл бұрын
Beautifully Explained!
@ritiksahu5310
@ritiksahu5310 Жыл бұрын
i think we can do it with time complexity 2^N - approach sort the array into decreasing order (NlogN) process leave -pick 2^N gocha, final complexity, NlogN +2^N ~= 2^N
@venkateshvenky2880
@venkateshvenky2880 Жыл бұрын
Understood clearly.
@nallapusrikar8490
@nallapusrikar8490 Жыл бұрын
I have a small doubt in line 4 -"ansList.add(new ArrayList(ds))", The ds is already an object of type ArrayList then we again create an object of type ArrayList by sending the parameter. why are we doing it?
@hx-ql3de
@hx-ql3de 10 ай бұрын
to avoid reference sharing
@adarshjaiswal7334
@adarshjaiswal7334 Жыл бұрын
What's the purpose of making deep copy here? How actually it required?
@Anonymous-q7t5c
@Anonymous-q7t5c 5 ай бұрын
00:00 Solve subset sum2 problem with no duplicate subsets 04:06 Access all courses and get a free certification exam with subscription 08:25 Generating all unique size 2 lists from a given list. 12:16 Generate all sublists of given list 15:55 Generating unique subsets with recursion 19:42 Time and space complexity of recursive function 23:19 Generate subsets of a given array 26:51 Explanation of recursive code with data structure
@rupamdwivedi9463
@rupamdwivedi9463 2 жыл бұрын
THANK U SOOOO MUCH FOR WONDERFUL EXPLANATION
@prabhakaran5542
@prabhakaran5542 3 ай бұрын
Understood ❤
@shambhurajeshirke2970
@shambhurajeshirke2970 2 ай бұрын
could you explain why there is no return statement in code ?
@vasanthanv6143
@vasanthanv6143 2 жыл бұрын
We are sorting the array, right? Why the time complexity for sorting the array is not taken into account? I know sorting is much better than storing the result into set and then converting into array List, but Arrays.sort() itself has some time complexity, right? Correct me if I am wrong.
@thomasShelbyLondon
@thomasShelbyLondon Жыл бұрын
total elements in nums are less then 10 as given in the constrains, so sorting 10 elements would not take much time
@amandwivedi2302
@amandwivedi2302 2 жыл бұрын
Using hashtable for the already visited will decrease the time complexity to ( 2**n +(n))
@Rajesh-op8zx
@Rajesh-op8zx 2 жыл бұрын
but will use extra space
@atharvakulkarni2024
@atharvakulkarni2024 3 жыл бұрын
I have a question? How would this recursion stop if theres no base case unable to understand?
@takeUforward
@takeUforward 3 жыл бұрын
Because the for loop will not get executed and hence no further recursion calls will be made
@bhanureddy2087
@bhanureddy2087 11 ай бұрын
i literally put the exact code and its not passing all the test cases in code studio i have gone threw the 3 times its the same
@rohandevaki4349
@rohandevaki4349 Жыл бұрын
isnt the time complexity n*n ? the first n is for visiting each element once and then reiterating it from 0 to n? how is it 2^n *n ?
@gourabrajak8132
@gourabrajak8132 Ай бұрын
u are the best striver,thnk u
@mohdkhaleeq7468
@mohdkhaleeq7468 2 жыл бұрын
in time complexity 2^n log 2^n is also added for sorting?
@parthsalat
@parthsalat 2 жыл бұрын
Thanks for the amazing explanation!
@jayadubey_22
@jayadubey_22 3 жыл бұрын
Why we have to do ds.popback() in recursion this I didn't understood bhaiya
@takeUforward
@takeUforward 3 жыл бұрын
before calling the recursion, you inserted it into ds, so when the recursion call ends, you need to remove it from ds, or for next calls it will be there. Hence pop back
@arlynsneha5052
@arlynsneha5052 3 жыл бұрын
@Jaya Dubey I too had the same doubt.... see it like this.. in the last rec call let the ds will be {1 2 3} ...now it should backtrack and go ryt so when it goes back it should go like{ 1 2} ... thats why we pop the last element
@avanisingh104
@avanisingh104 3 жыл бұрын
Bcs the recursion will go down and down first(and not in the right) and thus will return from the down function calls to upside and hence the ds needs to go back to it's previous state which was without the last element and so the last element is popped
@035asadali8
@035asadali8 2 жыл бұрын
bruh i dont know how i am solving all these question using recursion , i mean i am not thinking anything just write code and its work and when i draw tree or something i got confused ,anyone tell me what should i do
@harshavardhanmashetti5945
@harshavardhanmashetti5945 4 ай бұрын
what is the differnce between the combinations II and subset II videos.and another question what is the difference between the subsequence,subset,combinations
@reema7752
@reema7752 4 ай бұрын
in combination II, we need to print subsets with a sum of target only , but in subset II we need to print all possible subsets
@shanthiyasekar7317
@shanthiyasekar7317 Жыл бұрын
can anyone tell me what is the difference between combination sum problem and subset sum i feel both are same, i am not able to find the difference
@aryamankalia1228
@aryamankalia1228 Жыл бұрын
Is it just me , or is he just making a simple approach difficult by overexplaining it . because i swear to god i got the basic concept but the later mind boggling details are going over my head
@dewanandkumar8589
@dewanandkumar8589 3 ай бұрын
Understood
@aasthagoyal5059
@aasthagoyal5059 2 жыл бұрын
Why are we removing elements during backtracking .In explanation I don't find any point where I should remove element.Can anyone explain the reason?
@abhishekc3556
@abhishekc3556 2 жыл бұрын
you should draw the recursive tree and trace the recursion and you'll understand that there are some areas where you'll have to pop of the last element.
@vashishthsharma4411
@vashishthsharma4411 2 жыл бұрын
thank you bhaiya 😇😇😇😇
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir
@shefalibhattacharya3434
@shefalibhattacharya3434 4 ай бұрын
why are we removing the element from the list in LOC 9?
@Sahilsharma-sk5vr
@Sahilsharma-sk5vr Жыл бұрын
LEETCODE 90. Subsets II Given an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. TEST CASE 15 - EXPECTED VALUE IS WRONG? Input nums = [4,4,4,1,4] Use Testcase MY Output [[],[1],[1,4],[4],[4,1],[4,1,4],[4,4],[4,4,1],[4,4,1,4],[4,4,4],[4,4,4,1],[4,4,4,1,4],[4,4,4,4]] Expected [[],[1],[1,4],[1,4,4],[1,4,4,4],[1,4,4,4,4],[4],[4,4],[4,4,4],[4,4,4,4]] ^ ^ ^ | | |
@Rohitkumar-xo4pb
@Rohitkumar-xo4pb Жыл бұрын
please explain the i!=ind condition, i saw it again but didn't understood
@tasneemayham974
@tasneemayham974 Жыл бұрын
It's the same as the i>ind in the last video. We don't want duplicates, hence we check first if i==ind. If i is the same as the index, we break out of the if condition since that means we are choosing the first element. And we have to choose it since it's the first time we come across it. Now, if i>ind or i!=ind, then we check the other part which says nums[i]==nums[i-1] and if they are the same, we skip the i. One more thing, this i!=ind prevents a segmentation error when at nums[i-1] i is 0. This happens because i == ind, and so it will not check the nums[i-1]. I hope I made it clear.
@ShubhamKumar-km8pm
@ShubhamKumar-km8pm Жыл бұрын
Thanks striver sir for explaning it the best way possible💯💯
@kaichang8186
@kaichang8186 6 күн бұрын
understood, thanks for the perfect explanation
@shreyasingh1960
@shreyasingh1960 Жыл бұрын
I M ENJOYING THIS SERIES
@lostcyrus8578
@lostcyrus8578 Жыл бұрын
stll cant understand why you did i!=ind these both are same or not?
@saneetkaul8150
@saneetkaul8150 2 жыл бұрын
Pro tip :- Watch and Solve Combination 1 and 2 in sheet before solving this
@nayanverma7748
@nayanverma7748 Жыл бұрын
I have a doubt, why we haven't written statement in recursion
@rajdevanshu65
@rajdevanshu65 Жыл бұрын
Bhaiya uppar if mei index==nums.size() likh kar hi hum end mei 6th index tak pohonch paayenge else without the if part it just prints all the subsets.
@nipunrawat7137
@nipunrawat7137 Жыл бұрын
mtlb bhai?
@anshumaan1024
@anshumaan1024 Жыл бұрын
C++ code at 25:30
@nishanttiwari9736
@nishanttiwari9736 2 жыл бұрын
I have a doubt!! What if we remove the duplicates from nums array in the beginning itself
@smrutisouravsahoo972
@smrutisouravsahoo972 2 жыл бұрын
You will get wrong answers because while returning subset [2,2] is an answer which wont show up if you remove the duplicates at the early stage.
L12. Print all Permutations of a String/Array | Recursion | Approach - 1
19:07
L10. Subset Sum I | Recursion | C++ | Java
24:25
take U forward
Рет қаралды 363 М.
إخفاء الطعام سرًا تحت الطاولة للتناول لاحقًا 😏🍽️
00:28
حرف إبداعية للمنزل في 5 دقائق
Рет қаралды 59 МЛН
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 29 МЛН
«Кім тапқыр?» бағдарламасы
00:16
Balapan TV
Рет қаралды 106 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 672 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 114 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 583 М.
IIT-JEE Toppers: Where Are They Now?
15:52
Mohak Mangal
Рет қаралды 1,9 МЛН
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 250 М.
L7. All Kind of Patterns in Recursion | Print All | Print one | Count
34:12
Print unique subsets And Variations
24:48
Aditya Verma
Рет қаралды 107 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 816 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 425 М.
إخفاء الطعام سرًا تحت الطاولة للتناول لاحقًا 😏🍽️
00:28
حرف إبداعية للمنزل في 5 دقائق
Рет қаралды 59 МЛН