4. Print this pattern using Recursion-Write optimised recursive code that will impress interviewer🤭

preview_player
Показать описание
Let’s write some code together?

You can practice the question here-

Subscribe and hit the notification icon to get notifications and learn with me everyday!! ❤️❤️

#dsa #softwaredeveloper #interviewpreparation
Рекомендации по теме
Комментарии
Автор

We are going to practice many questions together. Keep up the energy with me ok?

codefromscratch-keertipurswani
Автор

Mam, I have watched all videos of your recursion. This problem was actually based on the previous problem, and i solved it with no flag and one recursion function only. Without any mistake. That is why because you are the best teacher of youtube...

jasmindersingh
Автор

what an awesome lecture, thank you for making recursion a lot easier ma'am🙏🙏

thenameisafsal
Автор

Able to write the optimized code by myself. Thanks 😍

unknowncoder
Автор

This is way better than I did by checking condition and adding flag. I have never thought it can be done with so less lines of code.
Thank you!!

skharel
Автор

SUMMARY


Approach 1: Using FLAG

Keep "inc" flag to keep note of increasing/decreasing scenario and push elements accordingly

void helper(vector<int> &res, int N, int present, int inc){

res.push_back(present);

if(inc){

if (present == N) return;
else helper(res, N, present+5, true);

}else{

if(present-5 > 0) helper(res, N, present-5, false);
else helper(res, N, present-5, true);

}

}

Approach 2: Without using FLAG

Go on printing while traversing down the tree and then upward

void helper(vector<int> &res, int N)
{
if (N <= 0) { res.push_back(N); return; }
res.push_back(N);
helper(res, N-5);
res.push_back(N);
}

simranvolunesia
Автор

Additional condition included for this problem in practice problem.
if(N <= 0) return {N};

if the number is less than or equal to zero return that number only. Otherwise print the pattern.

tjmanojofficial
Автор

This is beautiful!! Just played with the scope of the variable. I did it by checking my arraylist for termination and for recursion but this is way simpler and actually this is how it needs to be done. Thank you di!!😁

santhoshsai
Автор

Thanks, this refreshed my recursion concepts

shaikmansoor
Автор

I'll have to watch video was really nice.

shashwatraghuwanshi
Автор

Present. Stopped the vedio at 3:43 minute and tried myself once:

public class {

public static void main(String[] args) {
printNumbers(16);
}

private static void printNumbers(int n) {
if (n <= 0) {
System.out.println(n);
return;
}
System.out.println(n);
printNumbers(n - 5);
System.out.println(n);
}
}

arunkumarmohandas
Автор

Thank you so much mam! I was able to do it after you gave the hint.😅

nidhikumari
Автор

didi, ye cheez ekdam se dimag me nhi aati h, dekh ke bohot zyada seekhne ko mila

galepraveen
Автор

your hint that its same as we wrote yesterday gave the idea to write the a recursive function with just number as input. :)

nidhibansaliitr
Автор

present mam...Can you please guide that how to install thread library PC?

rtarts
Автор

After recursion series, please make video about time and space complexity

vignesh
Автор

ma'am can you also tell the same code in java?

Mitansh_Agrawal
Автор

Keerthi, I have been following your videos. Thank you for this. Can you please post the corresponding leetcode link / problem link as well . Thank you!

phalgunibumhyavarapu
Автор

It's hard still not got the logic, watching frm pt.1:)

theepiceditz
welcome to shbcf.ru