Re 2. Problems on Recursion | Strivers A2Z DSA Course

take U forward
24 Dec 202122:03

Summary

TLDRThis video, sponsored by Code Studio, offers a comprehensive guide to basic recursion problems, ideal for interview preparation. The presenter introduces over 2000 coding problems, solutions in C, Java, and Python, and guided paths for various topics. The tutorial focuses on recursive functions to print a name or numbers in a sequence, both linearly and in reverse, using parameters effectively without global variables. The explanation includes the concept of backtracking and emphasizes understanding recursion flow and complexity, ensuring viewers grasp the foundational concepts of recursion.

Takeaways

  • 😀 The video is sponsored by Code Studio, a platform with over 2000 interview problems and solutions in C, Java, and Python.
  • 📚 Code Studio offers guided paths for various subjects like Python, DBMS, OOP, OS, computer networks, system design, and web development.
  • 🏢 It also provides top company interview questions, such as Amazon's coding questions, and a collection of interview experiences from companies like Amazon, Microsoft, Adobe, and Google.
  • 🔗 There are free resources available for interview preparation on Code Studio, with a link provided in the video description.
  • 📈 The video focuses on basic recursion problems, building on a previous lecture about recursion.
  • 🔑 The first problem discussed is printing a name 'n' times using recursion, emphasizing the importance of base cases and recursive calls.
  • 🔄 The concept of changing parameters in recursion is introduced, with an example of printing a name 'n' times without using global variables.
  • 🕵️‍♂️ The video explains how recursion works with a step-by-step breakdown of function calls and returns, using the example of printing a name.
  • 📉 The time and space complexity of the recursion examples are discussed, highlighting that both are O(n) for the given problems.
  • 🔢 Additional recursion problems, such as printing numbers linearly from 1 to n and vice versa, are covered with a focus on understanding the logic behind the recursive calls.
  • 🔄 Backtracking is introduced as a recursion technique, demonstrated by printing numbers in increasing order but with the print statement placed after the recursive call.
  • 💡 The video challenges viewers to think about recursion in reverse order and to solve problems by starting from the end, encouraging active learning and participation.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is solving basic recursion problems in programming.

  • Who is the sponsor of the video?

    -The sponsor of the video is Code Studio, a platform for practicing interview problems.

  • What kind of problems does Code Studio offer solutions for?

    -Code Studio offers solutions for over 2000 problems in various topics including C, Java, Python, DBMS, OOP, OS, computer networks, system design, web development, and more.

  • How can viewers find top company interview questions on Code Studio?

    -Viewers can find top company interview questions via tags, such as 'Amazon', and get all the top coding questions along with their solutions in C, Java, and Python.

  • What additional resource does Code Studio provide for interview preparation?

    -Code Studio provides over 600 interview experiences from companies like Amazon, Microsoft, Adobe, and Google.

  • What is the first recursion problem discussed in the video?

    -The first recursion problem discussed is to print a name 'n' times using recursion.

  • What is the base case for the 'print name n times' recursion problem?

    -The base case for the 'print name n times' problem is when 'i' exceeds 'n', at which point the function returns and stops the recursion.

  • How does the video explain the concept of changing parameters in recursion?

    -The video explains changing parameters by demonstrating how to modify the function call to 'f(i + 1, n)' after printing the name, to continue the recursion until the base case is met.

  • What is the time complexity of the 'print name n times' recursion problem?

    -The time complexity of the 'print name n times' recursion problem is O(n), as the function is called 'n' times.

  • What is the space complexity of the 'print name n times' recursion problem?

    -The space complexity of the 'print name n times' recursion problem is also O(n), due to the stack space used by each recursive call waiting to be completed.

  • How does the video introduce the concept of backtracking in recursion?

    -The video introduces backtracking by not allowing the use of 'i + 1' and instead using 'i - 1' to print numbers from 'n' to '1', demonstrating how the print statement can be executed after the recursive call.

  • What is the challenge posed by the video for the viewers at the end?

    -The challenge posed by the video is to print numbers from 'n' to '1' using recursion without using 'i - 1, n' as the recursive call, and to share the solution in the comment section.

Outlines

00:00

🎥 Introduction and Sponsor Acknowledgement

The script starts with the host expressing gratitude to the sponsor, Code Studio, which offers a platform for practicing interview problems in various programming languages like C++, Java, and Python. The host highlights the platform's extensive library of over 2000 problems and guided paths for different programming topics, including Python, DBMS, OOP, OS, computer networks, system design, and web development. Additionally, the host mentions the availability of top company interview questions and shared experiences from interviews at companies like Amazon, Microsoft, Adobe, and Google. The host then invites viewers to check out the free resources for interview preparation provided by Code Studio, with a link in the video description. The video's main content is introduced as a continuation of a recursion playlist, focusing on basic recursion problems, and the host encourages viewers to watch the previous video for foundational knowledge before proceeding.

05:04

📚 Recursion Problem: Print Name N Times

The host delves into the first recursion problem, which involves printing a name N times using a recursive function. The explanation begins with a base case scenario where the function stops if the printed count exceeds N. The function is defined with parameters 'i' and 'n', starting from 1 and going up to 'n'. The host illustrates the recursive process with an example where 'n' is set to 3, demonstrating how the function calls itself with incremented 'i' values until it reaches the base case. The explanation includes a step-by-step breakdown of the function calls and prints, leading to the final output. The host also discusses the time and space complexity of the problem, concluding that both are O(N) due to the number of function calls and the stack space used during recursion.

10:07

🔢 Recursive Sequence Printing: 1 to N and N to 1

The host moves on to the next set of problems, which involve printing numbers linearly from 1 to N and then in reverse from N to 1 using recursion. The explanation for printing from 1 to N follows a similar recursive pattern as the previous problem, with the base case being when 'i' exceeds 'n'. The host then challenges the viewers to adapt the approach to print from N to 1, starting with 'i' equal to 'n' and decreasing it with each recursive call. The host provides a detailed explanation of how the function should be structured to achieve this, emphasizing the change in the recursive call from 'i + 1' to 'i - 1'. The examples given help clarify how the reverse order is achieved through recursion.

15:08

🔄 Backtracking in Recursion: Printing in Reverse Order

The host introduces the concept of backtracking in recursion, a technique used when the problem requires printing or processing in a reverse order without using the typical increment or decrement approach. The host presents a scenario where the print statement is placed after the recursive call, ensuring that the last call is executed first. This is demonstrated with an example where the function starts with 'i' equal to 'n' and decreases 'i' with each call, but the print statement is executed after the recursive call returns. The host walks through the execution flow, explaining how this approach allows for printing in increasing order even when starting from the highest value and recursively calling with decreasing values. The explanation includes a visual representation of the recursion tree, illustrating the order of function calls and prints.

20:11

🌟 Conclusion and Future Discussion

In the concluding part of the script, the host summarizes the problems discussed in the video, which include printing a name N times, printing numbers linearly from 1 to N and in reverse, and using backtracking to print in reverse order. The host encourages viewers to attempt a related problem as an exercise, hinting at a challenge to print from N to 1 without using the straightforward decrement approach. The host then invites viewers to like the video, subscribe to the channel if they are new, and check out the Striver SSD sheet linked in the description. The video wraps up with a reminder to stay tuned for the next video, which will cover more interesting recursion problems.

Mindmap

Keywords

💡Code Studio

Code Studio is mentioned as the sponsor of the video and a platform for practicing interview problems. It is defined as a resource with over 2000 problems and solutions in various programming languages such as C, Java, and Python. It is related to the video's theme as it provides guided paths for different programming topics and interview preparation, which is the main focus of the video content.

💡Recursion

Recursion is a fundamental concept in programming and the central theme of the video. It is defined as a process where a function calls itself as a subroutine. In the video, recursion is used to solve basic problems like printing a name or numbers in a sequence using a function that calls itself with modified parameters.

💡Base Case

The base case in recursion is a condition that stops the recursive calls and is crucial for avoiding infinite loops. In the video, the base case is used to determine when to stop the recursive function calls, such as when the counter variable exceeds a certain value, ensuring the function terminates after printing a name a specified number of times.

💡Recursive Function Call

A recursive function call is when a function calls itself within its own definition. In the script, the recursive function call is demonstrated with examples such as printing a name 'n' times or printing numbers from 1 to 'n'. The function call 'f(i+1, n)' or 'f(i-1, n)' is used to illustrate the recursion process.

💡Backtracking

Backtracking is a form of recursion where the function call is made first, followed by the execution of the print statement. It is introduced in the video as a technique to print numbers in a sequence starting from 'n' and decreasing to 1. The script uses the term to describe a different approach to recursion where the print statement is placed after the recursive call.

💡Time Complexity

Time complexity in the context of the video refers to the measure of how long an algorithm takes in terms of the amount of input. It is mentioned when discussing the efficiency of recursive solutions, with the video stating that the time complexity for the demonstrated recursive algorithms is O(n), indicating a linear relationship with the input size 'n'.

💡Space Complexity

Space complexity is the measure of the amount of memory an algorithm uses in relation to the input size. In the video, space complexity is also discussed as O(n) for the recursive algorithms, reflecting the stack space used by each recursive call until the base case is reached.

💡Interview Preparation

Interview preparation is a key theme in the video, as the script discusses using Code Studio for practicing coding problems that are often encountered in technical interviews. The term is used to highlight the practical application of the discussed recursion concepts in real-world scenarios such as job interviews at top companies.

💡Top Company Questions

Top company questions refer to the specific coding and technical questions that candidates might encounter during interviews with leading tech companies. The video mentions that Code Studio provides access to such questions, particularly for companies like Amazon, tagged and solved in multiple programming languages.

💡Guided Paths

Guided paths in the video refer to structured learning routes provided by Code Studio for various programming topics. These paths are designed to help users systematically prepare for technical interviews, covering areas such as Python, DBMS, object-oriented programming, operating systems, and more.

Highlights

Introduction of Code Studio as the sponsor of the video, offering over 2000 interview problems and solutions in C, Java, and Python.

Guided paths available for various programming topics including Python, DBMS, OOP, OS, computer networks, system design, and web development.

Access to top company interview questions and solutions, specifically mentioning Amazon coding questions.

Feature of reading interview experiences from companies like Amazon, Microsoft, Adobe, and Google.

Emphasis on the importance of watching the previous video on recursion before starting this lecture.

Explanation of solving basic recursion problems, starting with printing a name 'n' times using recursion.

Demonstration of a change of parameters in recursion by using integers 'i' and 'n' without global variables.

Illustration of the recursion process with a step-by-step example of printing a name 'n' times.

Introduction of time complexity and space complexity concepts in the context of recursion.

Solving the problem of printing numbers linearly from 1 to 'n' using recursion.

Approach to print numbers in reverse order from 'n' to 1 using recursion with a base case of 'i' less than 1.

Introduction of backtracking in recursion with an example of printing numbers from 'n' to 1 without using 'i + 1'.

Explanation of how to print numbers from 1 to 'n' by starting from 'n' and using backtracking.

Challenge to the audience to solve a recursion problem without using 'i + 1' and to share their solutions in the comments.

Visualization of the recursion process as a tree to understand the flow of function calls and returns.

Conclusion of the video with an invitation to like, subscribe, and check out the provided resources for further learning.

Transcripts

play00:00

so before starting this video i'd love

play00:01

to thank the sponsor of this video which

play00:02

is code studio now if you are willing to

play00:05

practice uh interview problems topic

play00:07

wise this is the place where you should

play00:08

look for because core studio has over

play00:10

2000 plus problems and has all the

play00:12

problem solution in c plus java as well

play00:15

as python if you're looking for a guided

play00:17

path then you can find for python for

play00:19

dbms for object oriented programming

play00:21

operating system computer networks

play00:23

system design web development any other

play00:25

thing that you're looking for you'll

play00:26

find guided paths for every other thing

play00:28

over here also also if you're looking

play00:30

for any top company questions let's say

play00:33

if you have an interview at amazon if

play00:35

you're looking for amazon questions you

play00:36

can get all the top amazon coding

play00:38

questions via tag and all the solutions

play00:40

in c plus java as well as python also if

play00:43

you have an interview schedule you can

play00:44

read their interview experiences where

play00:46

there are 600 plus interview experiences

play00:48

from companies like amazon microsoft

play00:50

adobe google etc so what are you waiting

play00:53

for code studio has a lot of free

play00:54

resources for interview preparation the

play00:56

link will be in the description make

play00:57

sure you check it out

play01:03

hey everyone welcome back to the channel

play01:05

i hope you guys are doing extremely well

play01:07

so today will be the lecture two of

play01:09

recursion playlist which is basic

play01:12

recursion problems now in the previous

play01:14

video we did learn about recursion in

play01:17

depth what is like so please make sure

play01:20

you watch out the previous video

play01:22

and after that only you start this video

play01:24

so in this video we are going to solve

play01:26

uh some problems the first one being a

play01:28

print name five times i'm assuming you

play01:31

can do that

play01:32

print uh

play01:33

linearly from one to n using recursion

play01:36

definitely

play01:37

print linearly from n to one then print

play01:40

linearly from one to n but in

play01:43

some backtracking over here i'll teach

play01:45

you how can you do stuffs

play01:47

uh using backtrack kind of backtrack

play01:50

and print from end to one again using

play01:53

back cracking okay so

play01:56

i'm assuming you can do the first three

play01:59

but still i'll recommend you to watch

play02:00

the entire video because

play02:02

the concepts gets more clear the more

play02:04

you watch the videos so let's uh take

play02:06

the first problem and do it that is uh

play02:09

print name

play02:11

n times let's keep it n times okay so

play02:16

the generic way is definitely uh

play02:19

to run a for loop and print the name n

play02:22

times but i want you to print your name

play02:25

n times using recursion yes that's

play02:28

that's a catch here using recursion

play02:31

so how can you do this

play02:33

you know there will definitely be a main

play02:35

which will uh be calling print

play02:38

okay so i'm saying n times so what you

play02:41

can do is you can take the n from the

play02:42

user

play02:44

yes you can take the n from the user

play02:47

so

play02:48

input as n in java it will be integer

play02:50

dot parsi and n

play02:53

and then you can say a function

play02:56

with

play02:57

uh probably 0 and n so over here i'm

play03:00

gonna teach you

play03:02

something which is very important uh one

play03:04

and then so this is the first time and

play03:06

i'm saying till new printed okay okay

play03:08

i'm gonna teach you something

play03:10

change of parameters which is very

play03:11

important so i'm writing a void f okay

play03:14

no global variables this time i'm taking

play03:17

an integer i

play03:18

and i'm taking an integer n

play03:20

okay

play03:21

over here i know the base cases

play03:24

if i start from yes if i start from 1

play03:27

i have to go till n that's for sure

play03:31

like i have to print it n times so i

play03:33

know the base case is definitely going

play03:35

to be till i haven't printed n times

play03:38

so if at any moment i

play03:41

exceeds n

play03:43

i am going to say return because

play03:45

i'm going to just print it n times

play03:47

that's very simple

play03:49

uh coming from the previous class it's

play03:50

very very simple this is what we call it

play03:52

as the base case

play03:54

remember

play03:56

so base case is decided on the problem

play03:58

statement so this is the base case

play04:02

okay

play04:02

what after that

play04:04

after that i will be like okay let's

play04:06

print it so you will simply say print

play04:10

let's assume my name is raj so i'll

play04:11

print raj

play04:13

and then i'll call f

play04:15

i've printed it once i did came up as

play04:18

one so the next time i will call it as

play04:21

i plus one comma n okay

play04:24

so let's understand how does the flow

play04:26

works so that you get a better

play04:28

perspective

play04:29

so initially uh you will be taking

play04:31

integer n

play04:32

assume n is given as 3 assuming n is

play04:36

given as 3 and again i'll put the output

play04:39

screen over here

play04:41

assuming n is given as 3

play04:43

so n becomes 3

play04:45

function says 1 and n

play04:47

so this guy calls this particular guy

play04:51

with i's value as 1 n's value as 3 so

play04:54

the function call yes the first function

play04:57

call that you make is of

play04:59

f of 1 comma 3 that's the first function

play05:03

call you made

play05:05

is the base case true no because

play05:08

1 is not greater than three

play05:10

one is not greater than three no

play05:13

thereby you say print raj so raj gets

play05:18

printed okay next you're saying function

play05:21

of i plus one so this guy calls a

play05:24

function with i plus 1 i was

play05:27

1 over here i gets incremented by 1 and

play05:30

calls the function by

play05:32

2 and n is still 3 and over here you

play05:35

write the same line of code i greater

play05:37

than n

play05:38

because it's the same function which is

play05:40

being called so in memory it's the same

play05:42

one

play05:43

print raj

play05:45

and

play05:46

then f of i plus one comma n

play05:49

okay

play05:49

this time let's understand if this base

play05:52

case is executed i is 2

play05:55

n is 3 so this will not be executed

play05:58

print raj

play05:59

again you print raj

play06:01

right remember this function of 1 of 3

play06:05

called this f 2 3 so in recursion tree

play06:07

it will be f of 2 comma 3

play06:09

this is how you create the recursion

play06:11

tree now f of 2 3 this is printed

play06:15

and now this portion so this guy goes

play06:17

and again calls f of i plus 1 so 3 comma

play06:20

3 and again you write the same lines is

play06:23

i greater than n

play06:26

then you say return

play06:27

definitely uh this is false so thereby

play06:30

you again go and say

play06:32

print raj

play06:35

and is f of

play06:36

i plus 1 comma n

play06:38

so this is false so this gets executed

play06:42

so raj is again printed

play06:44

f of i plus 1 so this time f like f of 2

play06:47

3 called f of 3 3

play06:49

so i can just

play06:50

draw that f of 2 3 called f of 3 3.

play06:53

now f of 3 3

play06:55

calls

play06:56

i plus 1 which is f of 4 comma 3

play06:59

and this moment i can say if i greater

play07:03

than n then return becomes true

play07:06

why because because

play07:08

i is4

play07:10

n is 3 and this guy comes out to be true

play07:13

thereby you are returning so the moment

play07:15

you're returning these lines will be of

play07:18

no use anymore these lines will be of no

play07:21

use anymore so you return now this is

play07:24

done now remember f of 3 3 called f of 4

play07:27

3 so you can do it in the recursion tree

play07:29

as well f of 3 3 called f of 4 3 okay

play07:33

so

play07:35

this is done

play07:36

this is over so i can say this function

play07:39

is over thereby you can return

play07:41

this is done so this function is over

play07:44

there by this is over you can return

play07:46

thereby this is over you can definitely

play07:49

return back to where it was called

play07:52

and now the function

play07:54

the program terminates so this is how

play07:57

easily the recursion did work and we did

play08:00

it in the parameters and not using any

play08:03

global variables so i was able to print

play08:05

my name n number of times is i was able

play08:08

to print my name n number of times and

play08:10

at f of 4 3 i returned so this got

play08:13

completed this return so this got

play08:15

completed this return so during the

play08:17

course this guy printed one raj this guy

play08:20

printed one raj this guy printed one raj

play08:23

and this guy hit the base condition and

play08:26

returned so this is how you can easily

play08:29

uh do the first problem which is print

play08:33

the name

play08:34

n times using recursion

play08:37

right i hope you have understood this

play08:39

so

play08:41

if i talk about the time complexity uh

play08:43

space complexity it's very very simple

play08:46

i can say the time complexity uh to be

play08:48

big o of n

play08:50

because inside the functions yes inside

play08:53

the functions uh these lines

play08:55

are like we go off one like they don't

play08:57

take any time so apparently uh you're

play09:00

calling one function

play09:01

that guy calls one more function that

play09:03

guy calls one more function so you're

play09:05

calling n functions if you carefully

play09:07

observe

play09:09

one two three four so you're calling n

play09:11

functions like here about n functions so

play09:14

i can say the time complexity to be big

play09:16

o of n yes i can say the time complexity

play09:18

to be b co of n

play09:20

and what is the stack space so in space

play09:22

complexity we generally assume the stack

play09:25

space now this guy would have been in

play09:27

the stack

play09:28

when you called

play09:29

f of 1 comma 3 would have been in the

play09:31

stack then f of 2 comma 3 would have

play09:33

been in the stack then f of 3 comma

play09:34

three would have been in the stack so

play09:36

they were they were waiting in the stack

play09:38

till this base case returned they were

play09:41

waiting to be completed so thereby i can

play09:43

say the space complexity is also big o

play09:46

of n now this space complexity is

play09:48

hypothetical like you're not using an

play09:50

array internal the computer's internal

play09:53

memory uses the stack space so thereby

play09:58

making the time complexity and the space

play10:00

complexity to be big o of n and b go

play10:02

often i hope that makes sense

play10:06

so guys we have done the first problem

play10:08

which is print the name ten times or

play10:11

five times okay now let's do the next

play10:14

problem which is print linearly

play10:16

from one

play10:18

to n

play10:28

so basically if n is given as four i

play10:32

want you to print one two three four yes

play10:34

this is what i want as the output

play10:36

okay so how will you do this again very

play10:39

much similar to the previous problem if

play10:41

i write down the function it's going to

play10:43

be i comma n and it's going to be i

play10:47

greater than n

play10:49

and you are saying return

play10:51

and this time the print statement will

play10:53

be executing i

play10:55

and then you can call f of i plus 1 and

play10:58

is a similar one just in place of the

play11:01

name you can print i

play11:03

right so that in the main yes so that in

play11:06

the main

play11:08

when you call the function like you can

play11:10

take the input of n

play11:12

you can take the input of n and then

play11:14

when you call the function with one

play11:16

comma n

play11:17

first time

play11:18

prince

play11:20

i which is

play11:23

one next time goes

play11:26

i plus one so two similarly you can just

play11:28

go on printing till whatever your n is

play11:31

so it was quite similar to the previous

play11:32

one where i'm easily able to print

play11:35

one

play11:36

to n correct now it's a time to uh think

play11:40

it in the reverse order and do the next

play11:43

problem which is

play11:44

print

play11:45

in the opposite fashion print in

play11:48

uh terms of

play11:52

n to one

play11:54

so

play11:55

like if i give you n equal to 4 you're

play11:57

going to print me 4 3 2 1. now can you

play12:00

do this can you do this i think you can

play12:04

yes you can uh it's very simple

play12:07

as of now the code that we wrote started

play12:09

from i equal to 1 and then you did i

play12:12

plus 1 then you did again i plus 1 and

play12:14

you kept on calling so if i'm if i have

play12:16

to print from 4 i can understand that

play12:19

the first

play12:20

i has to be 4

play12:22

and the last has to be a one

play12:26

after one

play12:27

before one you don't print so i can say

play12:29

i'll just write the function

play12:31

in the opposite order it's going to be

play12:33

same i comma n

play12:36

but this time it's going to be if i

play12:38

becomes

play12:39

lesser than 1 then i don't need to print

play12:42

and i can print the i

play12:46

and this time i can call it i minus 1

play12:49

comma n

play12:51

and during the start yes i repeat during

play12:54

the start of main

play12:56

i can take the input of n definitely

play13:00

i can take the input of n

play13:02

and i can say f of n comma n to be

play13:05

called yes i can say f of n comma n to

play13:08

be called so that if if assume n is

play13:11

given as 4

play13:12

the first time or assume the n is given

play13:14

as 3 to have a better understanding

play13:20

the first time you call it three comma

play13:22

three and this goes

play13:25

calls it like

play13:26

three comma three correct

play13:29

this line doesn't executes

play13:31

print three so the output screen will

play13:33

definitely have something like three and

play13:36

then you call it as f of two comma three

play13:39

and again then you will call it as f of

play13:42

one comma three and again at the end

play13:44

you'll call it f of zero comma three and

play13:46

this 0 comma 3 will be executed and

play13:49

you'll come back you'll come back and

play13:50

you'll come back so automatically you

play13:53

print 3 2 1

play13:55

as simple as that so it's the main stuff

play13:58

is you just change it over here where do

play14:01

you say i'm going to do a i minus 1

play14:07

this is the main step where you change

play14:09

that you say i'm going to do a i minus 1

play14:12

correct so again we have also done the

play14:14

next problem which is print in terms of

play14:16

n

play14:17

is

play14:18

to

play14:19

one

play14:24

so three problems have been done

play14:26

now

play14:28

these were very basic i want to do i

play14:30

want you to change it and think it in

play14:32

the reverse order

play14:33

what if i

play14:35

want to print from 1 to 1 but i don't

play14:37

want to do i plus 1 instead i say that

play14:40

the recursion call starts from n comma

play14:43

and usually like uh to give a better

play14:46

brief

play14:47

let me just explain i am saying print

play14:49

from 1 to n

play14:53

print from 1 to n

play14:55

but i am not going to allow you

play14:58

to use

play15:00

plus 1 like generally from 1 to n was

play15:03

meaning f of i plus 1 comma n was the

play15:06

function call was the recursive function

play15:08

call that you are making i'm not

play15:09

allowing you to do that i'm not allowing

play15:12

you to do plus one so how will you do

play15:14

this

play15:15

so over here i'm going to teach you

play15:16

something as backtracking okay so let's

play15:19

understand backtracking

play15:21

since i'm not allowing you to use plus

play15:23

you have to use minus so what i'll do is

play15:25

i comma n okay as usual this time it'll

play15:28

be like if i at any time goes lesser

play15:31

than one

play15:32

uh you return

play15:35

f of i minus one comma n

play15:38

and right after that i'm writing print

play15:40

of

play15:41

i okay

play15:42

so over here i'm writing the main

play15:44

function

play15:45

over here i'm writing the main function

play15:47

and

play15:48

what i'm writing is okay mean

play15:51

let's take the input of n

play15:54

and let's call f of n comma n

play15:56

so basically what i did was i said that

play15:59

you cannot use this plus thereby

play16:02

you can use minus one

play16:03

why because i wanted to i wanted you i

play16:06

wanted me to teach you

play16:08

what if i write the print line after the

play16:10

recursion call what happens let's

play16:12

understand so again let's uh quickly

play16:15

give the output screen

play16:17

and uh assume n is given as three so the

play16:20

first call is 3 comma 3 thereby the

play16:23

first call goes as

play16:25

3 comma

play16:27

3 the first call goes as 3 comma 3 now

play16:30

let's understand this does this line

play16:32

execute i lesser than 1 no because

play16:36

i is 3 and it doesn't executes

play16:39

perfect

play16:41

next

play16:42

i am saying f of i minus 1

play16:45

so this function call will go remember

play16:47

this

play16:48

the print line will not be executed

play16:51

the function call will go perfect let's

play16:53

call the function call and i'm saying i

play16:55

minus 1 that's 2 comma 3

play16:58

and again let's write if i is lesser

play17:00

than 1 then return

play17:03

let's say f of i minus 1 comma n

play17:06

and then let's write the print of i

play17:08

and then this okay so let's see if the

play17:11

first line executes again no because i

play17:13

is 2

play17:15

this time again you call the function

play17:17

with i minus 1 which is 2 minus 1 1 1

play17:20

comma 3

play17:22

and this time you write if i is lesser

play17:24

than 1

play17:25

you return

play17:26

and you say f of i minus 1 comma n

play17:30

print off i

play17:33

okay

play17:34

let's see what happens this time

play17:36

f of one comma three

play17:38

i is one doesn't executes

play17:40

f of i minus one

play17:43

goes and calls this with f of i minus

play17:46

one means zero comma three

play17:48

if

play17:49

i lesser than one return i'm not writing

play17:52

the remaining couple of lines

play17:55

i is zero so this line this time is

play17:59

indeed true

play18:01

is indeed true and if this line

play18:04

is

play18:05

true what happens

play18:07

you basically say return executes so you

play18:10

return

play18:11

so this function line has been executed

play18:15

correct now the print comes over here

play18:17

because after this

play18:19

the line of execution comes over here

play18:21

and it says print i

play18:23

what's the value of i

play18:25

one so the output will be one

play18:27

next

play18:28

the execution line comes over here which

play18:30

means the function has been executed

play18:33

correct

play18:34

go back

play18:36

went back

play18:38

this line has been executed

play18:40

thereby the function line comes to the

play18:42

next

play18:44

this print is i what's the value of i do

play18:49

print it

play18:50

next the line of execution goes here

play18:52

which means this function has been

play18:54

executed go back

play18:57

this line this has been executed you go

play18:59

to the next print i i's value is 3

play19:03

thereby this line has been executed

play19:06

go here

play19:07

hence go back

play19:08

so

play19:09

even by

play19:10

starting from 3

play19:12

even by starting from 3

play19:14

i was able to output

play19:16

in the linearly increasing order so it's

play19:19

not necessary that you have to start

play19:21

from

play19:22

1 in order to print from one two three

play19:24

you can start from the back and you can

play19:26

do the task after the function call so

play19:29

basically

play19:30

by doing this what you did was you made

play19:32

it mandatory that

play19:35

this line this print line is executed at

play19:37

the first

play19:38

because you kept it after the function

play19:40

call you kept it after the function call

play19:43

so

play19:44

unless until this function call is over

play19:46

this line would have never been executed

play19:48

so you made sure the last guy has been

play19:50

executed first

play19:52

why how did you make sure that the last

play19:55

guy because this was the last guy has

play19:56

been executed first how was this

play19:59

function completed first by making sure

play20:01

the print line is after the function

play20:03

call because this will only be executed

play20:05

if the base case is true thereby this

play20:08

gets executed this then then this gets

play20:11

executed then this that's the opposite

play20:13

way you made it by writing after the

play20:15

function

play20:16

got it so this is how you print from

play20:19

one is to n now i will be asking you to

play20:23

do the other one so this is what i've

play20:26

done but by backtrack

play20:28

this has been done i want you to do the

play20:29

other way

play20:30

i want you to print from end to end

play20:32

basically if i give you as n equal to

play20:36

3

play20:37

i want you to print 3 to 1 but i don't

play20:40

want you to use

play20:41

this i minus 1 comma n i don't want to

play20:44

do i don't want you to use this okay so

play20:48

i'm not solve this i want you people to

play20:50

answer in the comment section about this

play20:52

let's see if you have understood the

play20:53

entire concept you should be able to do

play20:55

it

play20:56

yes you should be able to do it so

play20:58

remember uh

play20:59

if i write this as a recursion tree it's

play21:01

very simple you started with f of three

play21:03

comma three then you went to f of two

play21:06

comma three then you went to f of one

play21:08

comma three then you went to f of uh

play21:10

zero comma three which indirectly stated

play21:13

a mover

play21:14

then the print line executed of one

play21:16

then you said

play21:18

after this the print of two was executed

play21:20

and right at so this the print of three

play21:23

was executed so this is how the

play21:24

recursion tree will look like for this

play21:26

particular stuff

play21:30

so i hope guys you have understood all

play21:33

the problems and you have understood the

play21:34

recursion in depth for the basic kind of

play21:37

problems of one 1d recursion so just in

play21:40

case you did please please please make

play21:41

sure you like this video and if you're

play21:43

new to this channel please do consider

play21:44

subscribing and yeah if you haven't

play21:45

checked out our striver ssd sheet the

play21:47

link is in the description please make

play21:48

sure to check it out with this let's

play21:50

wrap up this video and meet in the next

play21:51

one where i'll be discussing some other

play21:53

interesting problems bye

play21:58

don't ever forget your golden

Rate This

5.0 / 5 (0 votes)

相关标签
Recursion TutorialCode StudioInterview PrepCoding ProblemsGuided PathsTop CompaniesAmazon CodingMicrosoftAdobeGoogleProgramming
您是否需要英文摘要?