If Anyone Stuck in this ques with submission in Pep Portal : Simply use this, in starting of function if(sos > tar) return;
@tapans29984 жыл бұрын
thanks bhai
@rahulphutela91743 жыл бұрын
@Anugrah Johnson This condition is used to stop unnecessary Recursion calls. Please see below the working code: public static void printTargetSumSubsets(int[] arr, int idx, String set, int sos, int tar) { if(sos>tar) return ; if(idx == arr.length){ if(sos == tar){ set = set + "."; System.out.println(set); } return; } printTargetSumSubsets(arr, idx+1, set+arr[idx]+", ", sos+arr[idx], tar); printTargetSumSubsets(arr, idx+1, set, sos, tar); }
@geetanegi27363 жыл бұрын
@@rahulphutela9174 thank u bro😬
@bentennyson89233 жыл бұрын
@Anugrah Johnson for reducing time (avoiding TLE exception), and even if you continue there is not a solution as sos>tar
@ankitbhardwaj73633 жыл бұрын
Won't work if there are negative value(s) in array.
@ritikshandilya70757 ай бұрын
literally amazing !! recursion beginners folks can learn a lot from this video
@GauravKawatrakir3 жыл бұрын
If we able to saw the memory stack with recursion, its become more clear understanding.
@tarunkumar32792 жыл бұрын
Great man with a great solution and with a great initiative. All the best sir.
@pawanagrawal76533 жыл бұрын
instead of passing the idx pointer in the function , can we run a for loop from i=0 to n-1 and call the recursive the call from inside the for loop, and each time checking the base case if , sum==target or not , if sum==target print ans , and if sum>target then return ; .
@aashishgoyal14364 жыл бұрын
Thank u so much sir for solving different type of problems
@Pepcoding4 жыл бұрын
All the best
@AshishBaranwal-dl7fi Жыл бұрын
Sir, we can also optimize it using a if condition before recursion calls, if(sos > target) return;
@himanshudewan65353 жыл бұрын
if(sos > tar){ return; } Agar in future negative values hui tho we then cant find it
@avinashvikramjha33434 жыл бұрын
Very nice and informative..Thanku soooo much
@Pepcoding4 жыл бұрын
So nice of you
@Elon-musk-0074 жыл бұрын
sirji ek condition forget kar gye aap-----> if(sos>tar)return;
@Pepcoding4 жыл бұрын
Agreed.
@RajChahalNsit3 жыл бұрын
very easy to understand explanation ... thanks.. .
@sasidharnaidu45072 ай бұрын
Sir, why calculate the target sum at every call? Simply calculate the sum of final subsets in the base case. That way, we can reduce some calculation
@sagargarg49104 жыл бұрын
hello sir, i have tried the problem this way public static void printTargetSumSubsets(int[] arr, int startIndex, String set, int sum) { //special case if(sum == 0) { System.out.print(set+". "); return; } //base case if(startIndex == arr.length) { return; } if(sum < 0) { return; } printTargetSumSubsets(arr, startIndex+1, set+""+arr[startIndex]+", ", sum-arr[startIndex]); printTargetSumSubsets(arr, startIndex+1, set, sum); } here I am printing and returning when I counter the subset i.e sum becomes zero, it's slightly different than your code but I think the approach is the same. while submitting, it couldn't pass one test case and I can't think the test case on which the approach is not working. can you please help me understand what could go wrong
@sakshiaggarwal61993 жыл бұрын
Thank you so much sir!!
@gyandyan11 ай бұрын
#include using namespace std; // set is the subset // sos is sum of subset // tar is target void printTargetSumSubsets(vector arr, int idx, string set, int tar) { //write your code here if(tar==0){ coutn; for(int i=0;i>arr[i]; } cin>>tar; printTargetSumSubsets(arr,k,set,tar); return 0; }
@ShubhamSahu-fe8ed2 жыл бұрын
BEST 👏👏👏
@Pepcoding2 жыл бұрын
Keep learning. For better experience, visit nados.io, where you will get well curated content and career opportunities.
@nikhiljain30119 ай бұрын
I want tio know , if backtracking questions are also being covered by sumit sir ? Please confirm someone
@amandixit35554 жыл бұрын
sir jaise aap har recursion mei arr pass kar wa rahen hain , par tree mei har level par ek kam karte jaa rahe ho element , iska matlab har level par woh no. upar qtn mei bhi hoga aur neeche ans mei bhi, par hum convenience ke liye show nahi kar rahe hain upar ? sahi soch hai sir kya ?
@Pepcoding4 жыл бұрын
hnjii beta
@mukultaneja80144 жыл бұрын
Is subset equal to subsequence ? If not then if the sum we need is 30 , can we print 10 20 and 20 10 because both are subsets but latter is not subsequence ( does not maintain order )
@Pepcoding4 жыл бұрын
I use the two terms in same manner. Subset is to array as subsequence is to string.
@umangchhabra96784 жыл бұрын
sir 1 testcase is giving TLE.
@Pepcoding4 жыл бұрын
kahan pe?
@umangchhabra96784 жыл бұрын
@@Pepcoding pepcoding editor
@akshatpandey26094 жыл бұрын
Sir n=27 pass kiya gaya hai
@RaviKumar-kr4 жыл бұрын
include this base condition at top it will optimize the code if(sos>tar){ return;} and you will pass that test case of n=27.
@raghavbansal90204 жыл бұрын
Hello sir, v nice video. I tried solving it by myself. But, wrong answer. What's the issue sir, can u please have a look. It will be of much help. public static void printTargetSumSubsets(int[] arr, int idx, String set, int sos, int tar) { if (sos == tar){ System.out.println(set+"."); return; } if (sos > tar || idx >= arr.length){ return; } for (int i = idx; i < arr.length ; i++){ printTargetSumSubsets(arr, i+1, set+arr[i]+", ", sos + arr[i], tar); } }
@SCRIPTSAG4 жыл бұрын
Thank sir
@Pepcoding4 жыл бұрын
Glad you liked it!! share as much you can
@ankurtiwari55342 жыл бұрын
Sir agar if ka condition true ho jaye to return to terminate kar dega pure function niche wala recursion to use hi nahi ho payega
@Pepcoding2 жыл бұрын
For better insight, visit nados.io, post your doubts, community will help you out there.
@anirudhpanwar71423 жыл бұрын
Why didn't you use the frame work for recursion that you were using in the beginning ?
@sriramkrishnamurthy55283 жыл бұрын
Because these problems are about making choices and these type of questions are void recursion based questions where we explore multiple choices . In the beginning we solved questions which were not void recursion and they were questions which relied on the recursive solution for a smaller input for building a an output for a bigger input, those were faith-expectation problems and these are exhaustive choice exploration problems (backtracking) hence the same framework used in the earlier problems isn't used in these types of problems as these are predominantly two different types of recursive problems !!!!!!! Cheers bro ! 🔥👍
@dajindersingh95384 жыл бұрын
Sir, that one test case is still not resolved.
@Pepcoding4 жыл бұрын
Try it now
@AryanSharma-dh4fb3 жыл бұрын
this is subsequence not subset , right ?
@gauravgupta94774 жыл бұрын
Is there any trick to identify quickly if there exists overlapping subproblems??
@Pepcoding4 жыл бұрын
Recursion tree mei parameters ko dekho. Kahin same hain?
@manavmalhotra85133 жыл бұрын
target sum subsets using backtracking , vo kha hai?
@ankurtiwari55342 жыл бұрын
Wahi samajh nahi aa raha hai sir
@waez21203 жыл бұрын
if idx==len(arr): return if sos==tar: print(asf,'.') return and if idx==len(arr): if sos==tar: print(asf,'.') return else: return 1st Base is giving me incomplete answer. Can Someone explain me why?
@varunmodi56083 жыл бұрын
It is because when idx==len(arr), i.e., the last element is included and the sum has been reached. Your code is not accounting for that inclusion. In which case, you will have the correct answer, but on the position len(arr). The program just returns without ever checking if sos==tar. It thinks "one condition fulfilled, return". It has to check for the second condition as well. Hence the second if inside the first one. Hope you understand. Cheers mate.
@rolitiwari75273 жыл бұрын
sir I am not understand line no 28 or 29 code plzz explain the code
@Pepcoding3 жыл бұрын
For clearing such doubts you are most welcome on nados.pepcoding.com
@riaria59824 жыл бұрын
Dear Sir/All viewers, I have one question regarding the base case- why we haven't checked the base case like below: if(idx < arr.length) { if(sos==target) { System.out.println(set + "."); return; } } OR, we can do smart recursive call and lazy base case like below: if(sos==tar) { System.out.println(set + "."); return; } if(idx
@Pepcoding4 жыл бұрын
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding. Also, we have a premium facility available for the students in which you can get the 12 hours doubt support facility. Jisme aap agr kisi bhi question main kahin bhi faste ho to aap doubt support par reach kar skte ho aur aapko TA assign ho jayega and you can get your doubt resolved from them.
@gyandyan11 ай бұрын
u r correct bro, we can also do same as u mention above
@kartiksood82773 жыл бұрын
public static void printTargetSumSubsets(int[] arr, int idx, String set, int sos, int tar) { if(sos == tar){ System.out.println(set + "."); return; }else if(idx == arr.length || sos>tar){ return; } printTargetSumSubsets(arr,idx+1, set + arr[idx] + ", ",sos + arr[idx], tar ); printTargetSumSubsets(arr,idx+1, set,sos , tar ); } I used this logic. It is failing one test case.
@Pepcoding3 жыл бұрын
for such queries visit on to nados.pepcoding.com and post your query on community tab.
@divyaanshP3 жыл бұрын
#include #include using namespace std; void printTar(vector& coin, int tar, string asf, int idx){ if(tar == 0){ cout
@rishabhgoyal28354 жыл бұрын
sir hume sub-set nikalne the , lekin apne recursion sub sequence ka kia hai........... ab agar kisi din aise subsequence generate hogya ( jo non-continuous ho) or vo target ke barabar hogya ...to hum to print krdege ...... lekin ques me to subset ( must be continuous) manga hai.... what to do ?? confused ..!!!!!!!!!!!!!! @pepcoding
@Pepcoding4 жыл бұрын
beta subset aur subsequence same hota hai, non-continuous bhi ho sakta hai. subarray continuous hota hai.
@rishabhgoyal28354 жыл бұрын
@@Pepcoding thank you sir .. got it now !!! bhot pyaaara samajate ho aap