Re 3. Parameterised and Functional Recursion | Strivers A2Z DSA Course

take U forward
25 Dec 202118:14

Summary

TLDRThis video script introduces viewers to the concept of recursion through a problem-solving approach, focusing on summing the first 'n' numbers. The presenter explains two methods: parameterized recursion and functional recursion, using a simple example to demonstrate the process. They also touch on time and space complexity, and the importance of understanding recursive patterns for solving interview problems. The video is sponsored by Code Studio, offering resources for interview preparation, including problem solutions in various programming languages.

Takeaways

  • 😀 The video is sponsored by Code Studio, a platform offering over 2000 interview problems and solutions in various programming languages.
  • 📚 Code Studio provides guided paths for various programming topics including Python, DBMS, OOP, OS, computer networks, system design, and web development.
  • 🏢 The platform also features top company interview questions, such as Amazon coding questions, and over 600 interview experiences from companies like Amazon, Microsoft, Adobe, and Google.
  • 🔍 The speaker introduces a recursion problem: finding the sum of the first n numbers, and emphasizes the importance of understanding recursion for strong problem-solving skills.
  • 📈 The video demonstrates two methods for solving the summation problem: parameterized recursion and functional recursion.
  • 📝 Parameterized recursion involves passing additional parameters to the recursive function to carry state, while functional recursion relies on the function itself to return the answer.
  • 🔑 The base case for the summation problem is when n is less than one, at which point the function should return the accumulated sum.
  • 🔄 The recursive process involves calling the function with decremented values of n and accumulating the sum until the base case is reached.
  • 💡 The speaker explains the importance of learning recursion patterns for different scenarios, such as when to use parameters or returns effectively.
  • 📉 The video also touches on the time and space complexity of recursion, highlighting that the time complexity is O(n) and the space complexity is O(n) due to the call stack.
  • 🎓 The speaker concludes by encouraging viewers to practice recursion to master it and to like and subscribe for more educational content.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is teaching recursion through the example of summing the first n numbers using both parameterized and functional ways.

  • What is Code Studio and why is it mentioned in the video?

    -Code Studio is a platform that offers over 2000 interview problems and solutions in various programming languages, guided paths for different subjects, and interview experiences from top companies. It is mentioned as the sponsor of the video.

  • How many ways does the video discuss to solve the summation of the first n numbers?

    -The video discusses two ways to solve the summation problem: the parameterized way and the functional way.

  • What is the base case for the parameterized recursion method in summing the first n numbers?

    -The base case for the parameterized recursion method is when 'i' is less than one, at which point the summation is printed.

  • How does the functional recursion method differ from the parameterized method?

    -The functional recursion method does not carry additional parameters like the sum; instead, it returns the answer directly without printing it within the function.

  • What is the base case for the functional recursion method when summing the first n numbers?

    -The base case for the functional recursion method is when 'n' equals zero, at which point the function returns zero.

  • What is the time complexity of the recursive methods discussed in the video?

    -The time complexity of the recursive methods discussed is O(n), as there is one function call for each number up to n.

  • What is the space complexity of the recursive methods due to the function call stack?

    -The space complexity is O(n) due to the function call stack, as there will be n functions awaiting to be completed in the stack.

  • How does the video explain the transition from the parameterized way to the functional way in recursion?

    -The video explains that in the parameterized way, parameters are carried through each function call, whereas in the functional way, the function itself returns the answer, which is useful when the function needs to give back something instead of printing it.

  • What is an example of another recursive problem presented in the video?

    -The video presents the factorial of n as another example of a recursive problem, where the base case would be returning 1 when n equals 0.

  • Why is returning 0 as the base case incorrect for the factorial problem?

    -Returning 0 as the base case for the factorial problem is incorrect because it would result in all factorials being zero, as any number multiplied by zero is zero.

Outlines

00:00

📚 Introduction to Code Studio Sponsorship and Recursion Problem

The video begins by acknowledging the sponsorship of Code Studio, a platform offering over 2000 interview problems in various programming languages. It provides guided paths for different programming topics and top company interview questions. The speaker then introduces the main topic of the video: solving the summation of the first 'n' numbers using recursion. The goal is to strengthen the viewer's understanding of recursion through this interesting problem. The explanation is set to cover two approaches: a parameterized way and a functional way, with the latter being particularly useful for dynamic programming and returning values instead of printing them.

05:02

🔍 Deep Dive into Parameterized Recursion for Summation

This paragraph delves into the parameterized approach to recursion, where the function carries additional parameters to build the solution. The speaker explains how to implement a recursive function to calculate the sum of the first 'n' numbers, starting from 'n' and moving towards 1. The base case is when 'i' is less than one, at which point the sum is printed. The recursive case involves calling the function with 'i-1' and adding the current 'i' to the sum. The explanation includes a step-by-step breakdown of the recursive calls and how the function builds up to the final sum, using an example where 'n' equals 3.

10:05

📘 Transition to Functional Recursion and Problem-Solving Patterns

The speaker transitions to the functional approach to recursion, emphasizing the importance of understanding patterns in recursive problem-solving. The functional approach is particularly useful when a function needs to return a value rather than print it, as in dynamic programming scenarios. The paragraph explains the concept of a functional recursive function, where the function itself returns the answer. The base case is when 'n' equals zero, and the recursive case involves returning 'n' plus the recursive call to 'n-1'. The speaker illustrates this with the summation problem and then extends the discussion to the factorial problem, which involves a similar recursive pattern but with multiplication instead of addition.

15:06

🤔 Addressing Base Case Issues and Time/Space Complexity in Recursion

The final paragraph addresses a common mistake in recursive base cases, using the factorial problem as an example. It explains how an incorrect base case can lead to an erroneous result, such as returning zero instead of one in a multiplication scenario. The speaker corrects the base case and emphasizes the importance of setting it properly to avoid incorrect outcomes. Additionally, the paragraph touches on the time and space complexity of recursive solutions, noting that the time complexity is O(n) due to the number of function calls, and the space complexity is also O(n) due to the stack space used by the recursive calls awaiting completion.

Mindmap

Keywords

💡Code Studio

Code Studio is mentioned as the sponsor of the video, offering a platform for practice interview problems. It contains over 2000 problems with solutions in various programming languages such as C, Java, and Python. It is related to the video's theme as it provides resources for interview preparation, which is the main focus of the video content.

💡Recursion

Recursion is a key concept in the video, referring to a function that calls itself to solve smaller instances of the same problem. It is central to the video's theme of solving problems like the summation of the first n numbers and factorial of n using a recursive approach. The script provides examples of how to implement recursion in programming.

💡Summation

Summation in the video refers to the process of adding a sequence of numbers together to find the total sum. It is used to illustrate a problem that can be solved using recursion, where the script explains how to calculate the sum of the first n numbers both through parameterized and functional recursion.

💡Parameterized Recursion

Parameterized Recursion is a method of implementing recursion where parameters are carried through each recursive call. In the script, it is exemplified by a function that calculates the sum of the first n numbers by carrying the sum and the current number through each recursive call until the base case is reached.

💡Functional Recursion

Functional Recursion is a style of recursion where the function itself returns the answer rather than printing it. The script explains how to implement this by creating a function that calculates the sum of the first n numbers, where the function returns the result instead of printing it, which is useful for further calculations or in dynamic programming.

💡Base Case

The Base Case in recursion is the condition that stops the recursion from continuing infinitely. In the video, the base case is used to define when the recursive function should stop calling itself, such as when n is less than one for summation or n equals zero for factorial calculation.

💡Factorial

Factorial, denoted as n!, is the product of all positive integers up to n. It is introduced in the video as an example of a problem that can be solved using recursion. The script explains how to calculate the factorial of n using a recursive function, emphasizing the importance of the base case to avoid incorrect results.

💡Time Complexity

Time Complexity in the context of the video refers to the amount of time an algorithm takes to run, often expressed in terms of the size of the input. The script mentions that the time complexity of the recursive solutions for summation and factorial is O(n), as the function makes n recursive calls.

💡Space Complexity

Space Complexity is the amount of memory an algorithm uses in relation to the size of the input. The script discusses that the space complexity of the recursive approach is O(n) due to the stack space used by the recursive calls, where each call awaits the result of the next.

💡Dynamic Programming

Dynamic Programming is a technique used in algorithms to improve efficiency by storing the results of expensive function calls and reusing them when the same inputs occur again. Although not the main focus, the script briefly mentions dynamic programming in the context of wanting the recursive function to return a value for further use, rather than just printing it.

Highlights

Introduction of the sponsor, Code Studio, which offers over 2000 interview problems and solutions in multiple programming languages.

Code Studio provides guided paths for various programming topics including Python, DBMS, OOP, OS, computer networks, system design, and web development.

Availability of top company interview questions, such as Amazon, tagged for easy access to coding questions and solutions in C, Java, and Python.

Access to over 600 interview experiences from major companies like Amazon, Microsoft, Adobe, and Google on Code Studio.

The video focuses on solving the summation of the first n numbers using recursion, a technique to strengthen understanding of recursive programming.

Explanation of the parameterized way of recursion, where a function carries variables and performs calculations.

Demonstration of the functional way of recursion, where the function itself returns the answer without printing it.

The base case for the summation problem is when n is less than one, which stops the recursion.

Recursive function example where the sum is calculated by adding the current number to the sum of the previous number.

Illustration of the recursion process with a step-by-step breakdown of function calls and returns.

The importance of understanding recursion patterns for problem-solving in various programming scenarios.

Transition to the factorial problem as an example of another recursive problem, highlighting the change from addition to multiplication.

Clarification of the base case for factorial, which should return 1 instead of 0 to avoid incorrect multiplication results.

Discussion on the time complexity of the recursive solutions, which is O(n) due to the number of function calls.

Explanation of the space complexity of recursion, which involves the stack space used by the awaiting function calls.

Encouragement for viewers to like the video and subscribe to the channel for more content on programming and interviews.

Conclusion of the video with a summary of the key points covered and a reminder of the importance of recursion in programming.

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:04

hey everyone welcome back to the channel

play01:05

i hope you guys are doing extremely well

play01:07

today we will be uh solving a very very

play01:10

interesting problem in recursion which

play01:12

is the summation of

play01:15

the first n numbers

play01:17

now this can be done using

play01:21

a very simple for loop but i want you to

play01:23

do this using recursion because that

play01:25

will create your base of recursion very

play01:28

very strong i'm going to teach you a

play01:30

couple of ways the first way being the

play01:32

parameter wise ways

play01:35

where i'll be showing you how to do this

play01:37

using parameter the next one will be

play01:39

functional way where the function itself

play01:42

returns

play01:43

the answer okay so to start off with uh

play01:45

i hope you have understood the question

play01:46

sum of first n numbers assume n is given

play01:49

as 3 then the summation will be

play01:51

definitely 6 because

play01:53

1 plus 2 plus 3 is equal to 6 the sum of

play01:56

the first three numbers is what the

play01:58

question states

play02:00

so to start off with

play02:02

let's uh learn the parameterized way

play02:08

now when i say a parameterized wave what

play02:10

does that mean

play02:11

that means assume i write a function is

play02:14

assume i write a function and i say i

play02:17

and i carry a variable sum

play02:19

okay and that's the recursive function

play02:21

that i'm writing

play02:24

and i say that okay i'm gonna think this

play02:26

as a loop so in the previous lecture we

play02:29

saw that we started from one we went on

play02:31

to two we went down to three so we're

play02:34

gonna do this uh till we don't reach n

play02:36

or probably we can do it the opposite

play02:38

way from n to uh

play02:41

one so let's assume we do it from n to

play02:43

one so we can say if at any moment i is

play02:46

lesser than one yes if at any moment i

play02:48

is less than one

play02:50

i can definitely print yes i can

play02:54

definitely say let's uh print the

play02:57

summation

play02:58

okay

play02:59

and then try our return make sense

play03:03

and apart from this what i'm going to

play03:04

say is okay f of

play03:07

i minus 1

play03:09

sum

play03:10

plus i is what i'm going to pass

play03:12

so this is what my recursive statement

play03:15

will be and the main will be very simple

play03:17

again the main will be uh taking an n as

play03:20

the input

play03:21

and then we can just call f of

play03:24

n comma initial summation as

play03:27

0.

play03:28

now this is what i have taken now for an

play03:30

example let's assume this is the output

play03:33

screen

play03:35

and let's assume n is given as

play03:37

3

play03:38

so what i'm calling for the first time

play03:41

is

play03:41

i'm saying i to be

play03:44

3

play03:45

sum to be

play03:46

0

play03:48

right that is what i'm saying

play03:51

i to be 3 and sum to be 0

play03:54

this line is not executed

play03:57

this line is executed and it thereby

play03:59

calls another function

play04:02

in the same link the same function is

play04:03

called in the memory and it passes i

play04:05

minus 1 which is 2 because i was 3 so it

play04:09

tends down to 3 minus 1 which is 2 and

play04:12

sum plus i

play04:13

sum was 0 if you see sum was 0 so 0 plus

play04:18

3 will become 3 so it goes as

play04:22

3 again

play04:23

is the if line executed

play04:25

no the if line is not executed so the

play04:28

functional line will be executed and

play04:30

that's i minus 1 which means

play04:33

something and then sum was 3

play04:36

plus

play04:37

i this time is 2.

play04:40

now this time this function again calls

play04:42

a function which says function i minus 1

play04:45

so i was 2 it becomes

play04:48

1 and the summation 3 plus 2 becomes 5

play04:53

is the if statement executed no because

play04:56

the if statement if you carefully

play04:57

observe

play04:58

it's

play05:00

i lesser than one and that's not the

play05:02

case so it's not executed correct

play05:05

thereby

play05:07

it goes again and calls the same

play05:09

function with i minus 1

play05:11

comma

play05:12

sum plus

play05:14

i

play05:16

this time i minus 1 i was 1

play05:19

so this time the functional call is made

play05:21

to f of i minus 1 1 minus 1 becomes 0

play05:26

then summation summation was 5

play05:28

plus i i was 1 so it calls it as 6. what

play05:33

is the if statement if statement stated

play05:35

i lesser than 1 i less

play05:38

than 1 is that the case yes so what it

play05:41

does is

play05:42

print the summation summation was 6 so

play05:46

on the output screen the 6 is printed

play05:49

and it says

play05:51

return so thereby this functional line

play05:53

is no more executed and it directly

play05:55

returns from here

play05:57

so if it returns

play05:59

this function is done

play06:01

and now it returns

play06:03

thereby saying this function is done so

play06:06

this guy

play06:07

again returns

play06:09

thereby this function is done thereby

play06:11

this guy returns so we did a

play06:13

parameterized function where we can say

play06:16

that our recursive tree was

play06:19

3 comma 0 where this being the i and sum

play06:22

we went on to f of 2 comma added this 3

play06:26

we went on to f of 1 comma added this 2

play06:29

we went on to f comma 0 added this one

play06:32

so whenever we ended up at 0 we just

play06:35

printed this because we were

play06:38

adding the i adding the i every time to

play06:41

the parameter

play06:42

and that is what we printed this could

play06:45

have been done in the other way where

play06:46

you could have done i plus one as well

play06:48

but i'm just teaching you the parameter

play06:50

way in how can you actually carry the

play06:53

parameters yes because over here we

play06:55

increase the parameters and at the end

play06:57

of the day at the end of the day we came

play06:59

here and print this particular parameter

play07:02

which is six so this is how the

play07:04

recursion tree will be at the end it

play07:06

came back came back came back and went

play07:08

back right this is how you can do it

play07:10

using parameterized okay

play07:13

now if you've understood the

play07:15

parameterized it's time to understand

play07:17

the functional

play07:19

how do you generally

play07:21

write the function because functional

play07:23

means uh

play07:24

you don't want the parameter to do the

play07:26

work you want you write a recursive

play07:28

function where you say that hey listen

play07:30

this is my value of n take n as 3 and

play07:33

give me the summation as 6. so if i'm

play07:36

asking you to write such a function

play07:38

that's when you cannot use parameterized

play07:40

because you want the function to return

play07:41

the answer you don't want it to print

play07:43

you want it to return why

play07:45

uh

play07:46

there will be a lot of cases when you

play07:47

learn dynamic programming that you want

play07:49

the function to give you back something

play07:51

instead of printing right so basically

play07:54

uh to understand the concept before

play07:55

writing the recursive code let's uh so

play07:58

these are all patterns that i'm teaching

play07:59

it's not that i'm just writing the codes

play08:01

these are patterns i'm teaching you to

play08:03

learn these patterns if parameters are

play08:04

there then you have to do it in such a

play08:05

way if there's return then you have to

play08:07

do it in such a way to learn these

play08:09

patterns once you're very confident

play08:11

about these patterns you can solve any

play08:12

problem in recursion that is my word

play08:16

okay to go with functional let's

play08:18

understand when i say functional what

play08:19

does that mean now if n is given as a 3

play08:23

for a reason if n is given as 3

play08:25

can i say this can i say this

play08:28

i can say i'll do a 3 i'll do

play08:31

f of 2

play08:33

where i know where i know

play08:34

f of n means

play08:37

summation of

play08:39

first n numbers okay i know this so if i

play08:43

write

play08:44

3 plus f of 2 makes sense

play08:46

then when i'm calling for f of 2 can i

play08:48

say okay

play08:50

that can be written as 2 plus f of 1

play08:53

and when i'm at f of 1 can i write this

play08:55

as

play08:56

1 plus f of 0

play08:58

and everyone knows that f of 0 is

play09:00

nothing but 0 so i can think it in such

play09:02

a way that okay

play09:04

whenever there is an f of n

play09:06

i know n will be there

play09:08

i know n will be there which is 3

play09:10

because if i'm saying f of 3 i know the

play09:12

summation will contain 3 but i need the

play09:15

summation of the rest of the

play09:17

rest of the two numbers

play09:19

so this is the idea that i am going to

play09:22

follow so what i'll say is okay

play09:24

let them give me the n

play09:27

if at any moment n is equal to equal to

play09:29

zero i'm going to return a zero because

play09:32

if f is if n is zero i know the

play09:34

summation of the first 0 numbers is 0

play09:37

or else

play09:39

i'm going to return simply

play09:41

n plus f of n minus 1 okay

play09:45

i'm going to simply return n plus f of n

play09:49

minus

play09:50

1

play09:51

done

play09:53

now question

play09:54

might arise

play09:56

how will this work let's understand

play09:58

let's write the main function again so

play09:59

if i write main

play10:01

and i say n is given as 3 and i call f

play10:04

of 3 or f of n rather

play10:08

and i write

play10:09

print

play10:11

print this f of

play10:13

n

play10:15

so what is being done

play10:18

this f of n

play10:20

goes and calls this so let's call this

play10:24

with what value

play10:25

with the value 3 f of 3 is called is

play10:29

this line executed

play10:30

no

play10:31

n means

play10:33

3

play10:34

plus

play10:35

f of n minus 1 is called remember this

play10:39

there's a line

play10:40

in completed line 3 plus something

play10:43

incompleted that incompleted will be

play10:46

called in recursion till it does not

play10:48

comes i'm waiting three plus three plus

play10:50

something something i'm waiting got that

play10:53

so

play10:53

f of two now goes

play10:55

it states if n is equal to equal to zero

play10:58

it says no

play10:59

so i'm like okay return

play11:01

two plus f of one

play11:03

this time

play11:04

two plus

play11:05

this line

play11:07

waits

play11:09

for f of one to be done so i go and call

play11:11

f of one

play11:13

if n is equal to equal to 0 again i say

play11:16

no remember this you could have written

play11:18

n equal to equal to 1 that's your choice

play11:20

you can write it i'm just teaching you

play11:22

patterns that's it return 0 doesn't

play11:25

works

play11:27

return

play11:28

one plus

play11:30

f of zero

play11:31

again this line awaits i am awaiting let

play11:35

him come back

play11:36

so let's go

play11:37

f of zero

play11:39

when you go for f of zero let's

play11:41

understand what happens

play11:43

if n equal to equal to 0

play11:45

returns a 0

play11:47

this guy comes in with a value 0 returns

play11:51

a 0 this time i'm returning something

play11:53

i'm giving back something so it's

play11:55

returning a zero i hope that makes sense

play11:59

so whenever

play12:00

this function is called this function is

play12:02

called it came back with zero came back

play12:05

with zero so can i say

play12:07

now instead of writing this f of 0 i can

play12:10

actually write this 0 because this

play12:12

function yielded you a value 0 so

play12:15

thereby 1 plus 0 is 1

play12:18

hence return this guy returns a 1

play12:22

1 0 returns are one so can i say now

play12:24

instead of writing f of one

play12:26

can i write one because one has been

play12:29

returned

play12:30

thereby two plus one can i say this guy

play12:32

returns a three indeed i can say that f

play12:35

of n minus one goes away and i can write

play12:38

three so thereby three plus three is six

play12:41

thereby this guy returns six and you say

play12:43

print

play12:45

so output is six

play12:48

this is how yes this is how you can

play12:50

write a

play12:52

functional stuff when the problem is

play12:55

broken down into smaller parts yes

play12:58

the problem gets broken down into

play13:00

smaller smaller parts and once it is

play13:02

broken down it easily gives you the

play13:04

output

play13:06

so coming across to the code the quotes

play13:08

very very simple you say int sum you can

play13:11

call int n

play13:13

right and you can simply write if n is

play13:15

equal to equal to 0

play13:17

please

play13:19

return a 0

play13:21

else return an n plus sum of

play13:24

n minus 1 and over here probably i i can

play13:27

just keep n equal to a 3 you can take

play13:29

this take this as input as well c out

play13:32

sum of n so if i just write it you'll

play13:34

see this this perfectly works

play13:36

i saw it perfectly it's a very simple

play13:38

one okay so i hope you've understood how

play13:41

to get the sum of first n numbers

play13:43

now if i give you a very simple

play13:46

task task is

play13:49

factorial of

play13:52

n

play13:53

what is factorial of n

play13:55

whenever i give you n equal to 3

play13:58

it is 6 whenever i give you n equal to 4

play14:01

it's 24 basically 1 into 2 into 3 this

play14:05

time it's 1 into 2 into 3 into 4 correct

play14:10

multiplication instead of addition

play14:13

multiplication instead of addition so

play14:15

how will you do this

play14:17

i think that's very simple this time

play14:19

very simple

play14:20

uh

play14:21

if i write the code

play14:23

so i'll leave the parameterized you can

play14:25

do it yourself i'll try to write the

play14:27

recursive code okay now you know one

play14:30

thing for sure in the recursive code

play14:32

this will become a factorial

play14:34

correct

play14:36

and this will become into because 5 into

play14:39

4 into 3

play14:40

and this will become n minus 1 but we

play14:42

need to understand will the base case

play14:45

change

play14:46

will it be n equal to equal to zero the

play14:48

step let's see if i write this and if i

play14:50

run this what happens let's understand

play14:52

if i just give it a run

play14:54

see uh okay sorry let's give it a run so

play14:57

if i

play14:58

let's change it factorial okay now let's

play15:00

give it a run so if i give it a run you

play15:02

see the output being zero so you see the

play15:03

output being zero why

play15:05

due to this base case let's understand

play15:09

how why did this base gave uh

play15:11

why did this base case give you 0 so if

play15:13

i write f of n

play15:16

and i write the base cases n equal to

play15:18

equal to 0 and i say return

play15:20

0 and i'm saying return

play15:23

n

play15:24

into

play15:24

factorial of n minus 1 if this is the

play15:27

code

play15:28

and i am writing the main function as

play15:33

n is given as 3

play15:35

and i'm saying print

play15:37

f of n

play15:39

so basically this guy goes and calls

play15:41

this with three

play15:43

not executed

play15:45

three into

play15:47

f of

play15:48

two not executed

play15:50

if line is not executed so return

play15:54

2 into f of 2 minus 1 which is 1

play15:58

so this is gone f of 1

play16:01

again you're writing n equal to equal to

play16:02

0 not executed

play16:04

return 1 into

play16:07

f of

play16:08

0

play16:10

f of 0 is going

play16:13

and this time the if is executed

play16:15

and you return a 0 and you return a 0

play16:19

which is absolutely wrong because the

play16:21

moment you return on 0 this guy returns

play16:23

a 0 and if i just strike it off instead

play16:26

of f of 0 you end up writing a 0 which

play16:29

means 1 into 0 is 0 you get a 0 thereby

play16:33

this is straight up 0 2 into 0 is 0

play16:36

0

play16:38

this gets 0 3 into 0 is 0 you get a 3.

play16:41

so what should you have returned yes you

play16:44

should have written ideally 1

play16:47

or or or

play16:48

you should have done this

play16:50

this is also correct or even if you keep

play16:52

this this is also correct you should

play16:53

return one because in case of

play16:55

multiplications if you're returning 0

play16:57

the overall product gets multiplied to 0

play17:00

thereby giving you a wrong answer so

play17:02

this is what i wanted to

play17:05

explain you again uh what about the time

play17:07

complexity you're basically uh doing one

play17:10

call two call three call till n calls

play17:14

so i can say big of n

play17:16

is the time and the space complexity is

play17:19

an auxiliary space stack space because

play17:21

this awaits then this awaits so there

play17:24

will be go of n

play17:25

uh n functions will be awaiting to be

play17:28

completed so stack space will be taken

play17:30

so in stack uh this is what the time and

play17:32

space complexity will be y big of n as

play17:35

the time complexity first time this

play17:36

function is called next this time and

play17:38

these functions are all unique functions

play17:40

you don't take a lot of time inside

play17:42

these functions thereby the number of

play17:44

function calls will be equal to the

play17:45

number of time complex is equal to the

play17:47

time complexity so guys i hope you have

play17:50

understood the entire lecture so just in

play17:52

case you did please please please make

play17:53

sure you like this video and if you're

play17:55

new to the channel please do consider

play17:57

subscribing please please do consider

play17:59

subscribing and yeah with this let's

play18:01

wrap up this video and meet in the next

play18:03

precaution video bye-bye

play18:05

[Music]

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
RecursionCoding ProblemsInterview PrepCode StudioC LanguageJavaPythonSummation AlgorithmFactorialTechnical Learning
Benötigen Sie eine Zusammenfassung auf Englisch?