1 00:00:00,750 --> 00:00:02,460 Hi, everyone, welcome back. 2 00:00:02,490 --> 00:00:08,640 So in this video, we are going to write the code for the function flip, so let's start writing the 3 00:00:08,640 --> 00:00:08,960 code. 4 00:00:10,860 --> 00:00:17,160 So first of all, we need to return one vector and this vector will contain two values left and right. 5 00:00:17,640 --> 00:00:21,900 So let's create one vector vector of integers. 6 00:00:22,560 --> 00:00:23,660 Let's answer. 7 00:00:24,480 --> 00:00:32,250 So it will be Offsiders two and let's say the initial values, let's initialize to minus one. 8 00:00:32,250 --> 00:00:34,170 Minus one is an invalid index. 9 00:00:34,590 --> 00:00:35,060 Right. 10 00:00:35,100 --> 00:00:45,090 So what this line will do so this line will create one array of size two and initialize all the values 11 00:00:45,090 --> 00:00:47,420 that is initialized to values by minus one. 12 00:00:47,430 --> 00:00:47,790 Right. 13 00:00:48,120 --> 00:00:48,810 Very simple. 14 00:00:49,080 --> 00:00:54,020 So now what we discussed, we need to find out the value of left and right. 15 00:00:54,030 --> 00:01:02,970 So let's take two variables left, which is initially zero, obviously, and let's take one variable 16 00:01:02,970 --> 00:01:04,319 rate zero. 17 00:01:04,319 --> 00:01:07,230 So we need to find out the values of left and right. 18 00:01:08,140 --> 00:01:13,350 Let's also find out the number of elements the size of the string. 19 00:01:14,010 --> 00:01:16,260 So which is a size 20 00:01:19,710 --> 00:01:21,410 and for goodness algorithm. 21 00:01:21,900 --> 00:01:28,380 We discussed that we need one variable maximum sum, which will be initially minus infinity. 22 00:01:29,010 --> 00:01:33,630 We need one variable, Göransson, which is initially zero. 23 00:01:34,440 --> 00:01:37,890 And then if you remember, we need to iterate over the area 24 00:01:41,610 --> 00:01:42,930 and what we used to do. 25 00:01:43,590 --> 00:01:51,290 So we used to write like this current sum plus equals to a of a. 26 00:01:51,450 --> 00:01:54,260 But now this line will change how this line will change. 27 00:01:54,780 --> 00:02:01,170 So basically we discussed that if this is my string zero zero one one one, then what I am going to 28 00:02:01,170 --> 00:02:06,590 do, I will convert zero into one and I will convert one end to minus one. 29 00:02:06,750 --> 00:02:10,919 So that means my new string will be one one minus one minus one minus one. 30 00:02:11,070 --> 00:02:16,590 And I need to apply Caden's algorithm on this new string, newly created string. 31 00:02:16,600 --> 00:02:16,980 Right. 32 00:02:17,700 --> 00:02:24,840 So one way is basically you are ideato with the string and you manually change a zero to one and one, 33 00:02:24,840 --> 00:02:25,850 two minus one. 34 00:02:25,860 --> 00:02:27,050 So that's the one way. 35 00:02:27,180 --> 00:02:32,460 But there's no need to do that y because whenever we will encounter zero, what we will do. 36 00:02:32,760 --> 00:02:37,650 So ultimately what we will do, we will find out the maximum sum summary for this. 37 00:02:38,020 --> 00:02:38,480 Right. 38 00:02:38,670 --> 00:02:42,420 So whenever we will encounter zero, we will add one. 39 00:02:42,810 --> 00:02:45,750 Whenever we will encounter one, we will add minus one. 40 00:02:46,320 --> 00:02:46,790 Right. 41 00:02:47,640 --> 00:02:49,790 So no need to change the original string. 42 00:02:50,220 --> 00:02:51,420 So what we will do. 43 00:02:52,340 --> 00:03:02,090 Whenever we will encounter, whenever we encounter zero, what we will do, I will add, plus one, 44 00:03:03,410 --> 00:03:03,880 right? 45 00:03:03,900 --> 00:03:07,580 So current some place close to one. 46 00:03:08,900 --> 00:03:19,550 So whenever I encounter zero hour and in the ls part, whenever we encounter one, we will add minus 47 00:03:19,550 --> 00:03:20,840 one to current. 48 00:03:20,840 --> 00:03:24,890 Some plus equals two minus one or reconnoitering some minus close to one. 49 00:03:25,950 --> 00:03:26,390 Right. 50 00:03:27,170 --> 00:03:32,190 Whenever you encounter zero and one whenever you encounter one at minus one. 51 00:03:32,210 --> 00:03:34,470 So that is what I am doing here late. 52 00:03:34,760 --> 00:03:38,350 So no need to write this line and then it's very simple now. 53 00:03:38,840 --> 00:03:42,020 So what we need to do first of all, let's remove this line. 54 00:03:45,750 --> 00:03:55,410 OK, so what we need to do if the current sum is greater than the maximum sum, so we need to update 55 00:03:55,410 --> 00:03:58,580 our make some some this is standard guidance algorithm. 56 00:03:58,980 --> 00:04:03,690 So let's update the maximum sum, which is equal to the current sum. 57 00:04:05,230 --> 00:04:05,690 Fine. 58 00:04:06,130 --> 00:04:13,270 And we also need to find out we also need to update the right so right will be the current index. 59 00:04:14,530 --> 00:04:16,290 We are updating the maximum sum. 60 00:04:16,510 --> 00:04:21,640 So what we will do, we will update our answer also to answer of zero that is left. 61 00:04:21,940 --> 00:04:23,410 So what be the value of left? 62 00:04:23,410 --> 00:04:26,380 It will be left plus one. 63 00:04:26,410 --> 00:04:29,700 Remember, we need to return our answer as one index. 64 00:04:29,940 --> 00:04:34,060 So that's why I'm adding one and similarly answer of one that will contain. 65 00:04:34,060 --> 00:04:34,480 Right. 66 00:04:35,080 --> 00:04:37,660 So the right will be the value of right. 67 00:04:38,080 --> 00:04:38,880 Plus one. 68 00:04:39,070 --> 00:04:41,560 We need to report our answer by adding one. 69 00:04:41,920 --> 00:04:51,390 And similarly, the next part of them is if current sum becomes zero, then you need to make some equals 70 00:04:51,400 --> 00:04:51,760 zero. 71 00:04:51,970 --> 00:04:54,120 And also you will update your left. 72 00:04:55,930 --> 00:04:59,560 So your left will be a plus one. 73 00:05:00,100 --> 00:05:00,670 Simple. 74 00:05:01,060 --> 00:05:02,800 And finally, what do. 75 00:05:04,810 --> 00:05:08,540 Finally, we will return our answer and that's it. 76 00:05:08,680 --> 00:05:10,090 So will the scorebook. 77 00:05:10,660 --> 00:05:13,420 So I think there is a very tiny problem with this code. 78 00:05:13,420 --> 00:05:14,480 And what is that problem? 79 00:05:14,770 --> 00:05:19,200 So if my string is containing all the ones when one and when. 80 00:05:20,200 --> 00:05:23,470 So what is my vector, my what is my area? 81 00:05:23,470 --> 00:05:25,390 So my area is containing two values. 82 00:05:25,390 --> 00:05:26,830 The value of left is minus one. 83 00:05:26,830 --> 00:05:28,190 The value of is minus one. 84 00:05:28,600 --> 00:05:30,910 So when will apply cadenced algorithm. 85 00:05:30,920 --> 00:05:34,060 So this will be string minus one, minus one and minus one. 86 00:05:34,400 --> 00:05:34,840 Right. 87 00:05:35,080 --> 00:05:38,950 So my Caden's algorithm, what it does, it returns the maximum sum. 88 00:05:38,950 --> 00:05:44,770 Sabeti And what is the maximum Zanzibari minus one is the maximum sum. 89 00:05:44,770 --> 00:05:46,960 Sabeti Right. 90 00:05:47,110 --> 00:05:53,620 So whatever array will contain it will contain the value of left plus one and the value of right plus 91 00:05:53,620 --> 00:05:53,770 one. 92 00:05:53,800 --> 00:05:56,710 So that means this index rate when Gormogon. 93 00:05:56,950 --> 00:06:01,360 So you will report your answer is when Gormogon because this is the maximum Saberi. 94 00:06:01,690 --> 00:06:04,450 So the value of left will with this value of right will be this. 95 00:06:05,260 --> 00:06:06,830 So is this answer correct? 96 00:06:07,090 --> 00:06:11,040 No, we discussed that of string is containing all the ones. 97 00:06:11,410 --> 00:06:17,530 Then your answer array must be empty because in that case you will not apply flip operation. 98 00:06:17,800 --> 00:06:23,830 If you will not apply flip operation, then the value of left and right will not exist and your area 99 00:06:23,830 --> 00:06:24,700 will be empty. 100 00:06:25,530 --> 00:06:25,890 Right. 101 00:06:25,930 --> 00:06:28,930 But in this code, according to this code, what this code will return. 102 00:06:28,930 --> 00:06:29,760 It will return one. 103 00:06:32,500 --> 00:06:37,050 So what we can do, we need to handle this case and how we can handle this case. 104 00:06:37,480 --> 00:06:38,500 Very, very simple. 105 00:06:38,770 --> 00:06:45,910 So before returning the answer, we will have one check that if the maximum sum is minus one. 106 00:06:45,940 --> 00:06:48,280 So in this case, what is the maximum? 107 00:06:48,580 --> 00:06:50,170 Minus one is the maximum sum. 108 00:06:50,440 --> 00:06:57,880 So if the maximum sum is minus one, that means all the values in the string are one, all the values 109 00:06:57,880 --> 00:06:59,020 in the string I want. 110 00:06:59,020 --> 00:07:02,730 And in that case, you will basically clear your area. 111 00:07:02,740 --> 00:07:06,060 You will delete both these entries and you will then empty adding. 112 00:07:07,840 --> 00:07:08,250 Right. 113 00:07:08,380 --> 00:07:18,430 So before returning your answer, you need to have one check that if the value of maximalism is equal 114 00:07:18,430 --> 00:07:21,610 to minus one, that means your string is containing all ones. 115 00:07:22,030 --> 00:07:26,170 And if your string is containing all ones, then you need to return ampitheater. 116 00:07:26,590 --> 00:07:30,160 And for returning ampitheater, we have a function on Sadaat Clear. 117 00:07:31,420 --> 00:07:36,910 So what this clear function does, it will remove all the elements from the error rate. 118 00:07:37,690 --> 00:07:41,950 So we will clear our area and then we will return our answer. 119 00:07:42,820 --> 00:07:47,110 So let's see whether our code passes the basic test cases or not. 120 00:07:50,370 --> 00:07:51,150 OK, so 121 00:07:53,850 --> 00:07:56,070 I brought this by mistake. 122 00:08:02,640 --> 00:08:07,830 Yep, so our goal is passing basic discuses now let's submit our code. 123 00:08:12,580 --> 00:08:20,110 Yeah, so basically our court has passed all the test cases and it was just the variation of cadenced 124 00:08:20,110 --> 00:08:20,700 algorithm. 125 00:08:20,950 --> 00:08:23,810 So the code is very simple and very easy to understand. 126 00:08:23,830 --> 00:08:25,150 So I will share the code with you. 127 00:08:25,150 --> 00:08:27,760 And if you face any problem, so do let me know. 128 00:08:27,770 --> 00:08:29,390 I will be more than happy to help you out. 129 00:08:29,650 --> 00:08:31,470 So this is all about this free device. 130 00:08:31,480 --> 00:08:36,159 I will see you with more interesting question in our next videos so that the guys that. 131 00:08:36,210 --> 00:08:36,640 Bye bye. 132 00:08:36,669 --> 00:08:37,210 Thank you.