1 00:00:01,870 --> 00:00:02,650 Hi, everyone. 2 00:00:02,680 --> 00:00:06,950 So in this video, we are going to solve this question, so we need to find out. 3 00:00:06,970 --> 00:00:10,630 We need to find out the number of ways to reach and from zero. 4 00:00:10,990 --> 00:00:12,520 So basically, these are the steps. 5 00:00:15,820 --> 00:00:20,620 So I want to reach step and starting from ground and ground zero. 6 00:00:20,650 --> 00:00:25,390 OK, so ground zero, I want to step in and I can take a jump off. 7 00:00:25,390 --> 00:00:28,310 When I can take a jump of two, I can take a jump of three. 8 00:00:28,540 --> 00:00:34,240 So from here I can take a jump of one, I can take a jump of two and I can take a jump of three. 9 00:00:35,560 --> 00:00:40,760 So we need to find out how many number of bees are there, for example, if we want to reach three. 10 00:00:41,530 --> 00:00:45,850 So number of ways to reach trees, basically, you can take a jump of one, one and one. 11 00:00:46,690 --> 00:00:52,060 Similarly, it can take a jump of one, then take a jump of the first trigger, jump of two, then take 12 00:00:52,060 --> 00:00:54,520 a jump of one and one more directly. 13 00:00:54,520 --> 00:00:57,760 Take a jump of three from zero DIKLA take a jump of three. 14 00:00:58,150 --> 00:01:00,910 So to reach three from zero. 15 00:01:02,090 --> 00:01:03,140 I have four of his. 16 00:01:05,489 --> 00:01:08,820 OK, so for viewers out there, if the value of an E t. 17 00:01:10,280 --> 00:01:11,840 So I hope you understood my question. 18 00:01:11,990 --> 00:01:16,700 So given the value, often you have to tell me how many number of these are there. 19 00:01:17,630 --> 00:01:20,310 So this question can be easily solved with the help of recursion. 20 00:01:21,230 --> 00:01:22,520 So what resurgences? 21 00:01:23,770 --> 00:01:25,660 See, you want to reach an. 22 00:01:27,230 --> 00:01:33,590 You want to reach in and you can take a jump of 120 so you can reach in from minus one. 23 00:01:35,040 --> 00:01:40,440 You can reach in from Minnesota, you can reach GNR from ministry. 24 00:01:42,370 --> 00:01:48,190 So the number of ways to reach in will basically be the addition of these three to a number of ways 25 00:01:48,190 --> 00:01:52,810 to reach and is basically number of ways to reach and minus one, because if you are standing at 10 26 00:01:52,840 --> 00:01:57,640 minus one, you will take a jump of one plus the number of ways to reach. 27 00:01:57,640 --> 00:02:03,160 And minus two, if you are standing at minus two, you will take a jump of two and you will reach and 28 00:02:03,250 --> 00:02:07,330 if you will take a jump of one, then that will be counted in the number of ways to reach and minus 29 00:02:07,330 --> 00:02:08,949 one, which we have already counted. 30 00:02:09,320 --> 00:02:11,770 Similarly, number of ways to reach. 31 00:02:11,770 --> 00:02:17,500 And minus three, if you are standing at then minus three, then you need to take a jump of three to 32 00:02:17,500 --> 00:02:20,410 reach and OK, you do not have any choice. 33 00:02:20,410 --> 00:02:22,390 You will take a jump of tea if you will. 34 00:02:22,390 --> 00:02:25,480 See I am standing at then minus three and I take a jump of one. 35 00:02:25,900 --> 00:02:30,520 It will take a jump of one, then it will be counted as number of ways to reach and minus two and it 36 00:02:30,520 --> 00:02:31,190 is already there. 37 00:02:31,540 --> 00:02:36,790 So a number of ways to reach and is basically number of ways to reach and minus one number of ways to 38 00:02:36,790 --> 00:02:39,850 reach and minus two and number of ways to reach and minus three. 39 00:02:39,850 --> 00:02:43,200 And the addition of these three will give you the number of ways to reach. 40 00:02:43,210 --> 00:02:46,060 And so, for example, let's take one example. 41 00:02:49,360 --> 00:02:53,920 So a number of ways to reach one is one. 42 00:02:55,100 --> 00:02:56,430 Take one step from zero. 43 00:02:56,960 --> 00:03:03,710 Number of ways to each two is basically I can take a jump of 21 and I can take a jump of the number 44 00:03:03,710 --> 00:03:06,750 of ways to reach two is two and above. 45 00:03:06,800 --> 00:03:09,800 We calculated that number of ways to each three is basically four. 46 00:03:10,280 --> 00:03:10,820 Simple. 47 00:03:11,360 --> 00:03:13,360 And what is the number of ways to reach zero? 48 00:03:13,970 --> 00:03:17,010 Basically you are standing at zero and you want to reach zero. 49 00:03:17,210 --> 00:03:18,110 So there is only one. 50 00:03:18,110 --> 00:03:19,250 We do not do anything. 51 00:03:20,260 --> 00:03:24,030 So now let's try to find out how many number of ways to reach for. 52 00:03:24,310 --> 00:03:26,910 I want to reach Stepha's, how many of these are there? 53 00:03:27,610 --> 00:03:34,350 So you want to reach for so you can reach for and the gems allow this one to entry so you can reach 54 00:03:34,360 --> 00:03:38,590 Plumtree, you can reach from two and you can reach from one to four three. 55 00:03:38,830 --> 00:03:43,690 You can reach from two, you can reach three from one and you can reach three from zero. 56 00:03:43,840 --> 00:03:44,340 Simple. 57 00:03:44,830 --> 00:03:47,530 So what we will do, we will make it as a base case. 58 00:03:47,530 --> 00:03:50,230 We know a number of ways to each do is to. 59 00:03:50,590 --> 00:03:54,310 So I will return to a number of ways to each one is basically one. 60 00:03:54,520 --> 00:03:57,730 So I will return one number of ways to reach zero. 61 00:03:57,730 --> 00:03:58,900 And you are standing at zero. 62 00:03:58,940 --> 00:03:59,880 So do not do anything. 63 00:04:00,220 --> 00:04:01,100 There's only one way. 64 00:04:01,330 --> 00:04:06,130 So two plus three plus the number of ways to reach three is four I will return for. 65 00:04:07,340 --> 00:04:14,010 Number of ways to reach do is to so I will return to a number of ways, each one is basically one, 66 00:04:14,280 --> 00:04:15,270 so I will return one. 67 00:04:15,810 --> 00:04:19,410 So what I will do, I will take the additional four plus two, six and seven. 68 00:04:19,660 --> 00:04:21,690 So a number of ways to reach 47. 69 00:04:22,950 --> 00:04:29,520 It is seven simple, so a number of ways to reach for this number of mysteries, three plus number of 70 00:04:29,520 --> 00:04:32,040 ways to reach to less number of ways to reach one. 71 00:04:32,400 --> 00:04:38,070 So a number of ways to ease trees for a number of ways to each tool is basically do a number of ways 72 00:04:38,070 --> 00:04:38,990 to each one is one. 73 00:04:39,180 --> 00:04:41,220 So four plus two plus one is seven. 74 00:04:41,370 --> 00:04:42,470 Seven is the right answer. 75 00:04:42,810 --> 00:04:45,390 And what are those ways you want to reach for. 76 00:04:45,690 --> 00:04:47,910 So one, two, three and four. 77 00:04:48,450 --> 00:04:50,970 One, two and four. 78 00:04:51,330 --> 00:04:54,060 One, one, three and four. 79 00:04:55,410 --> 00:04:56,610 Similar to one one. 80 00:04:58,460 --> 00:05:02,180 And one entry, three, anyone, one, two, one, two. 81 00:05:02,690 --> 00:05:06,400 So let's count one, two, three, four, five, six, seven. 82 00:05:06,590 --> 00:05:09,680 So seven ways are there to each step four. 83 00:05:10,840 --> 00:05:13,380 So this question can easily be solved with the help of the. 84 00:05:13,840 --> 00:05:16,930 So what do you want to reach and write? 85 00:05:17,530 --> 00:05:25,000 So to reach in, you can reach from minus one, you can reach from minus two and you can reach from 86 00:05:25,120 --> 00:05:25,690 minus three. 87 00:05:26,770 --> 00:05:32,200 So what you will do, you will apply recursion for then minus one applied equation, applied equation. 88 00:05:32,440 --> 00:05:36,580 And of course it will give me the number of ways to reach and minus one, let's call it X and of course 89 00:05:36,580 --> 00:05:40,750 it will give you the number of ways to reach and minus two, let's call it Y because it will give you 90 00:05:40,750 --> 00:05:45,430 the number of ways to reach and minus three, let's call it Z and with a number of ways to reach an 91 00:05:45,580 --> 00:05:47,920 addition of X plus Y plussed. 92 00:05:48,400 --> 00:05:48,910 Simple. 93 00:05:49,420 --> 00:05:50,530 So let's write the code. 94 00:05:54,280 --> 00:05:57,310 So in, let's say, the name of the function, this count is. 95 00:05:59,880 --> 00:06:07,800 So I want to reach in from zero what will the case, because it's very simple, if you are standing 96 00:06:07,800 --> 00:06:08,280 at zero. 97 00:06:09,460 --> 00:06:11,260 Or us standing at one. 98 00:06:12,170 --> 00:06:13,970 So in that case, do not do anything. 99 00:06:15,440 --> 00:06:18,080 So there is only one way I will return one. 100 00:06:19,620 --> 00:06:22,200 Otherwise, what will my answer? 101 00:06:22,920 --> 00:06:23,790 My answer will be. 102 00:06:26,130 --> 00:06:33,250 Number of ways to reach and minus one, plus number of ways to reach and minus two. 103 00:06:34,650 --> 00:06:37,950 Plus, a number of ways to reach and ministry. 104 00:06:38,950 --> 00:06:42,610 And I think our function will be OK, so let's test our function. 105 00:06:45,500 --> 00:06:48,890 So integer and it's taken Porten. 106 00:06:51,120 --> 00:06:52,770 And let's call the function see out. 107 00:06:53,950 --> 00:06:55,570 Countries need to give an. 108 00:06:56,660 --> 00:07:00,860 So our function will work, but there's one slight problem with our function and what is that slight 109 00:07:00,860 --> 00:07:01,260 problem? 110 00:07:01,940 --> 00:07:03,520 So let's take this example. 111 00:07:04,250 --> 00:07:05,060 So, for example. 112 00:07:07,150 --> 00:07:12,600 Number of ways to reach to the value of any number of ways to reach to this one and one and to take 113 00:07:12,600 --> 00:07:15,430 a jump of two or take a jump of one on one, simple. 114 00:07:15,780 --> 00:07:21,060 So if the value of it is to let supply and so who is not and is not and is not one. 115 00:07:21,750 --> 00:07:24,990 So we will call on one. 116 00:07:26,210 --> 00:07:30,120 We will call on zero and two will call on minus one. 117 00:07:31,480 --> 00:07:36,880 So this is a case we know the answer is one, this is a base case, we know the answer is one. 118 00:07:38,670 --> 00:07:40,180 But you are calling one minus one. 119 00:07:40,950 --> 00:07:45,920 So what will happen minus one is not a biscuit, so it will call on this, it will call on minus two, 120 00:07:46,740 --> 00:07:50,150 it will go on minus three, and it will call on minus four. 121 00:07:50,760 --> 00:07:52,630 And similarly, these are not biscuits. 122 00:07:52,860 --> 00:07:53,940 So, again, it will call. 123 00:07:54,900 --> 00:07:55,500 On three. 124 00:07:56,620 --> 00:08:02,260 So Tuticorin, Colin Taseko also, it will call on minus three, minus four, minus five, similarly 125 00:08:02,530 --> 00:08:07,010 minus four, minus five, minus six and minus five, minus six, minus seven. 126 00:08:07,030 --> 00:08:08,020 So it will continue. 127 00:08:08,530 --> 00:08:08,900 OK. 128 00:08:08,920 --> 00:08:10,400 So this will become infinite. 129 00:08:11,050 --> 00:08:12,010 So what we need to do. 130 00:08:13,050 --> 00:08:17,500 So we need to make it as a base case, we need to take it as a base case, OK? 131 00:08:17,760 --> 00:08:22,410 So we need to make it as our base case so that we do not reach minus one. 132 00:08:23,340 --> 00:08:25,540 So we need to make it as our base case. 133 00:08:25,560 --> 00:08:26,910 So it is compulsion here. 134 00:08:27,480 --> 00:08:29,490 We need to take to as our base case. 135 00:08:29,760 --> 00:08:31,890 So this is the first option to handle the situation. 136 00:08:32,169 --> 00:08:35,669 The second option is to handle the situation is basically you can write one condition. 137 00:08:35,970 --> 00:08:39,270 If the value of it becomes less than zero, return zero. 138 00:08:41,020 --> 00:08:46,090 So this is the second we saw minus one is basically zero, so I will return zero, so I will return 139 00:08:46,090 --> 00:08:51,620 zero from you so you can make two as your best case or you can use this condition. 140 00:08:51,850 --> 00:08:56,740 So what I want to say to is calling on when we know the answer to is calling on zero. 141 00:08:56,740 --> 00:09:01,600 We know the answer because both our base case, but who is also calling one minus one? 142 00:09:02,130 --> 00:09:04,660 So I told you, there are two ways to handle the situation. 143 00:09:04,840 --> 00:09:09,420 First, make two as our base case so I can write one condition here. 144 00:09:09,460 --> 00:09:13,150 I can add one more base case if the value of any two I know. 145 00:09:13,150 --> 00:09:13,990 What is the answer? 146 00:09:13,990 --> 00:09:14,770 Answer is two. 147 00:09:14,950 --> 00:09:15,790 I know the answer. 148 00:09:17,120 --> 00:09:23,420 So I can make it as a base case, so then I will reach out to I will directly answer to Sorto will act 149 00:09:23,420 --> 00:09:24,110 as a base case. 150 00:09:24,800 --> 00:09:30,360 The second situation is we can right this condition if the value is less than zero. 151 00:09:30,590 --> 00:09:32,360 So basically, this is invalid value. 152 00:09:32,630 --> 00:09:34,360 OK, this is invalid value. 153 00:09:34,580 --> 00:09:37,100 So for the invalid value, my answer will be zero. 154 00:09:38,090 --> 00:09:42,400 So if you recall, and minus and minus one is invalid value, so I will return zero from here. 155 00:09:42,680 --> 00:09:44,710 So you have both options. 156 00:09:44,720 --> 00:09:48,830 You can make it you can make a call like this or you can make a call like this. 157 00:09:49,220 --> 00:09:50,130 It's your choice. 158 00:09:52,550 --> 00:09:58,340 So first, which is basically you can write like this, you need to make to your best case. 159 00:09:59,300 --> 00:10:00,260 And we know the answer. 160 00:10:00,260 --> 00:10:01,040 Answer is to. 161 00:10:02,420 --> 00:10:03,010 Answer is. 162 00:10:03,940 --> 00:10:06,580 So now your code will work, so let's test our function. 163 00:10:08,050 --> 00:10:12,670 So a number of ways to reach three is basically Ford, so Ford is the right answer. 164 00:10:13,920 --> 00:10:21,090 Similarly, the number of ways to reach forty seven, so a number of ways to reach four is seven, so 165 00:10:21,090 --> 00:10:24,950 seven is the right answer and we already discussed it here. 166 00:10:24,960 --> 00:10:27,240 So a number of ways to Utah is basically seven. 167 00:10:28,710 --> 00:10:33,510 So this is the first way of making your record correct, the second way of making your record correct 168 00:10:33,510 --> 00:10:38,100 is if you do not want to make it as your best case, what you can do, you can add one condition. 169 00:10:39,440 --> 00:10:40,700 And what is that condition? 170 00:10:41,960 --> 00:10:42,440 If. 171 00:10:43,590 --> 00:10:45,510 The value often is basically invested. 172 00:10:46,610 --> 00:10:52,530 If the value of wine is invalid, then in that case, that answer will be zero for anybody to value. 173 00:10:52,550 --> 00:10:53,480 My answer will be zero. 174 00:10:55,330 --> 00:10:59,710 I want to search for for no obvious reason for this seven, it is working. 175 00:11:01,370 --> 00:11:07,640 Similarly, I want to reach out to a number of races, too, and it is working, so you have the option. 176 00:11:08,530 --> 00:11:10,810 You can use this or you can use this. 177 00:11:12,000 --> 00:11:13,450 Or you can use both of them. 178 00:11:14,550 --> 00:11:17,460 So this is it from this video, I will see you in the next one.