• 2 days ago
Transcript
00:00Next question, which we will talk about is largest subarray with zero sum. So, I have to take out the
00:05length of the subarray, which will be the sum of the subarray, which will be the sum of the subarray,
00:10which will be the sum of the subarray, which will be the sum of the subarray, which will be the
00:20sum of the subarray, which will be the sum of the subarray, which will be the sum of the subarray,
00:25which will be the sum of the subarray, which will be the sum of the subarray.
00:28minus eight, minus eight, plus one minus seven, plus seven equals zero. So, this sub-airage, which is the sum
00:36of zero, another sub-airage is created here, minus two and two. This sub-airage also is the sum of
00:42zero. And this sub-airage also is the sum of zero, which I have minus eight, one and seven. So,
00:48there are three sub-airage which is the sum
00:50I have 0, but who is the largest? The largest is this one, which is the length of 5. So
00:56I will print here 5. If I give another area, which is 3, 4, 5. Now what are all the
01:02elements in this area? Positive. So if we add any element here, the sum is never 0. So in this
01:09case, I have to print 0, or 0 length, which means no sub-array exists, which is the sum of
01:15all elements.
01:16Now we can solve it with many different approaches. If we discuss the brute force approach, what will I do?
01:24I can solve it using nested loops. What will I do in nested loops? I will put an outer loop
01:29for int i equals 0, i less than n, i plus plus. What will I do? I will pick one
01:35element and will calculate all the arrays from that. So I will put a loop for int
01:43j equals i plus one and j ko mein lekar jaungi j less than n tak j plus plus, to
01:50har bar
01:51I will calculate 15 for 15 and minus 2. Then I will calculate 15 for 15, minus 2, 2, and
01:572. Then I will calculate 15, minus 2, 2, minus 8. So I will calculate all the sum from 15
02:03to 15. After the next iteration, I will calculate all the sum from 2 to 2, minus 8. Then I
02:10will calculate all the sum from 2 to 2. Then I will calculate all the sum from 8.
02:20In this nested loop process, the time will be equal to n2. How do I need to solve this approach
02:25in a linear approach? I want to solve this in a linear approach. And when there are such questions, which
02:30are sub-arrays, I have some sum, usually there are 3-4 variety of questions in which we use hash
02:38maps or hash sets. So we also solve this question in linear time using hash maps. This approach will be
02:46For the approach, first of all, let's do a little bit of clarity. Let's say that I have an array
02:52in which I have index i. Let's say this is my index i. What did I do? I have sum
02:59stored from 0 index to ith index. And I have sum stored from 0 index to jth index. Let's say
03:07this is my index i. And this is my index here. And this is my index jth index.
03:21Then that will be equal to sum of 0 to j minus sum of 0 to i. Where I know
03:29that j is always bigger from i. For example, let's assume that I have taken i here. This is my
03:36i which is equal to 2 and this is my j which is equal to 4.
03:40Now sum of 0 with i, what will this value be? 15 minus 2 plus 2, this is equal to
03:4715. And with 0 with 4, what will this value be? Sum of 0 to j. This value will be
03:5515, I will do it with 15, so I will do it with 7. 7 plus 1 is 8, so
04:01here the value will be equal to 8. Now I will remove these values, and what will I do?
04:08If this was i and this was j, then if I want to find out the values of this area,
04:16then I will start directly from i plus 1. This is i plus 1. Or j to go. Or I
04:22can do this sum from this sum to minus.
04:25I mean, 8, if I have minus 15, this value is equal to minus 7. So, I can say minus
04:328 and 1's sum is minus 7. So, what did I do? I used the pre-sum property. Pre-sum,
04:39we already used the array list in many questions. And what is the property? Directly, if I plus 1 from
04:47J, I will remove the sum from 0 to J.
04:54Now, what should I logically? I want to find out such sub-arrays. I want to find out such sub
05:00-arrays. So, I want to find out such sub-arrays. So, I want to find out such sub-arrays. So,
05:08what can I say? In that case, my sum of j minus sum of i will be equal to 0
05:15or equal. In that case, the sum of j should be equal to sum of i.
05:23I will derive such questions where I have to use hash map for my target sum. What is my target
05:28sum here? The target sum is equal to 0. So I have to find out a sub-array with the
05:33sum of 0i and I know what the sum of any sub-array is. It is sum of 0 to
05:39j minus sum of 0 to i. Now if it has 0 value, then in that case, my sum of
05:44j and sum of i should be equal. If I understand logically, this means that my total part is
05:52The first part is the first part. The second part should be equal to these two. So, if the first
05:55part is equal to the first part, then the second part is equal to 0. Let's take this example. In
06:02this case, if I take 0 and my J take 5 equal to this case, what will happen? My sum
06:13of I will be equal to 15 and my sum of J will be equal to 15.
06:22minus 2 is 13, plus 2 is 15, minus 8 is 7, plus 1 is 8, and plus 7 is
06:2915. So, again, sum j will be 15. I mean, if I calculate such a sum, then it will be
06:3515. And if I calculate this whole sum, then it will be 15.
06:41equal. So I can say that there was so many array that I had already given 15 sum. If I
06:47had mixed all parts of the elements, then sum is 15. This means that the first element of the first
06:53element, after all these numbers, they are not giving any contribution to the sum. Their total sum is 0, so
07:00the final sum is 15. So I can say that sum of my i plus 1 from j to the
07:07values.
07:080 will come. So, the sum of this subarray will come. That is equal to my 0 value. So, I
07:13have to use this property. What property? I have to summarize this property. I knew that with i plus 1,
07:19I have to take the sum of j to the subarray. So, I will take the sum of j to
07:22the subarray. So, I will take the sum of j to the subarray. I will take the sum of j
07:24to the subarray. Now, when I need 0, then in that case, my sum of j to the subarray should
07:30be equal to my sum of i to the subarray. Which I have shown you an example. Now, how do
07:35we use this property using hash maps?
07:38What do I need to do in the question? I need to make the largest sub array, which sum is
07:44equal to 0. So for this reason, I will track a hash map, which I am going to track within.
07:59which is my length which will be initialized with 0
08:17Now, I will store each unique sum along with its index. First, I will go to index 0. In index
08:240, my sum will increase 15. Now, I will check if 15 already
08:30the map exists in this map. If there is a 15 map exists in this map, what do I do?
08:35I am running my j loop. I mean sum of j, if sum of i exists in these two indexes,
08:42there is a sub-array that the sum is 0. In this case, my sub-array length will be j
08:50-i equal.
08:52Now the map is empty, so this will not exist. So I will store it in the map. So I
08:56will store it as my sum. Which index has come? Index has come for j is equal to 0. So
09:03here it will be 0. Now I will come to next index. Now I am coming to minus 2. This
09:08is my j loop.
09:10Minus 2 के लिए मैंने sum के अंदर minus 2 को add किया तो यह value होगी 13 अब 13
09:15के लिए check करूंगी क्या 13 मेरे map के अंदर exist करता है 13 map के अंदर exist नहीं करता
09:20है तो 13 को add करवा दूंगी 13 बन जाएगा नया sum इसका index हो जाएगा 1 के equal अब
09:2513 से बढ़कर मैं एक आगे आ जाओंगी 2 पर 2 को add कर दूं�
09:31Now, I will check 15 to check if the sum exists in the map. What did I get to know?
09:41I got to know that there is a sub-array whose size is J-I equal. J is here 2.
09:50And what is i? i is this index, which is 0. So, there is a sub-array, which is a
09:56sub-array. Which is the sub-array? Which is the sub-array? Which is the sub-array? Which is the
09:59sub-array? Which is the sub-array? Which is the sub-array? Which is the sub-array? Which is the
10:02sub-array? Which is the sub-array? Which is the sub-array? Which is the sub-array? Which is the
10:06sub-array? Which is the sub-array? Which is the sub-array?
10:12The second time, when I have index of 2, I mean, I mean, I mean, I mean, I mean, I
10:18mean, I mean, I mean, I mean, I mean, I mean, I mean, I mean, I mean, I mean, I
10:21mean, I mean, I mean,
10:39minus this i is equal to 0. 2 means 0 minus to length which is equal to 2. Now I
10:44compare this length to my answer. My answer is equal to 2.
10:49Because this sub array is greater than 0, so it will be 2. Now, do I need to add this
10:5415 again? I don't need to add it again. Because if I have put it again in the map, it
11:00will be 2. Next time, I will get a small sub array. I want to find out the largest sub
11:07array. So, for the largest sub array, I need to start the same sum from the smallest possible index. That's
11:14why I won't update it here, if it already exists.
11:18pseudocode भी लिख लेती हूं कि मैं क्या कर रही हूं हर बार मैं एक loop चला रही हूं for
11:22int i is equal to zero to my array की length या मैं इसको कह सकती हूं i is equal
11:27to zero to n हर बार check करती हूं कि sum exist करता है in map तो मुझे क्या करना
11:35है तो मुझे simply length के लिए compare करना है अपनी length को अपने j minus i से यह actually
11:44मेरा j का loop है
11:45j loop 1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1
11:48-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1
11:49-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1
11:50-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1
11:52-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1
11:57-1-1-1-1-1-1-1-1-1-1-1-1-1-
12:14Map.put sum and my index j. This is my code. Now, I am going to the next index to
12:21minus 8. As I am going to this minus 8, I am going to this minus 8. Now, my sum
12:28will be 15 minus 8, which is equal to 7. Now, I will check if 7 already exists in map
12:32or does not exist in that case. So, I will add 7 to 7. So, I will add 7 to
12:387 index which is equal to
12:413. Then I will come to my next index. Next index is 1. I have added sum to this value.
12:478 will be equal to 8. I will check for 8. Does 8 already exist or not? So I will
12:52add 8. I will add 8 with index 4. Now I will come to next index. 7 will be equal
13:00to this value. I have added 7 to 8. So what happened? I have sum to 15. Now I will
13:06check if 15 already exists.
13:10In my map, I checked 15, so 15 already exists. Where is 15? 15 is index i is equal to
13:170. If it already exists, then I have to take out j-i. Here I have a second sub-array,
13:25which has j-i value. 5 is equal to i, 15 is equal to 0. So j-i will be
13:35equal to 5.
13:36So, I compare the length with j-i and the length will be bigger. So, this will be updated to
13:41the value of 5. If I can understand this more logically, then I can think that the 15 sum already
13:47exists. Which sum sum? This sum sum. The index i is equal to 0. Now, what did I get? j
13:53is equal to 5. I got another sum sum which is 15.
13:59j is equal to 5, so this sum is also 15. So how can I say that this small sum
14:04is 15 and the bigger sum is 15. I mean this sub array which is length j minus i is
14:10equal, its sum is equal to 0 and its length will be 5 which I have stored as my largest
14:16sub array with sum 0. Now I will come to my next index. What will I do here? This is
14:22my j is equal to
14:236. Now the sum will be 15 plus 10 which is equal to 25. 25 exists not that, so 25
14:30I have added for index 6. Last time, I will be stored in my map, but what is my final
14:36answer? This length is my final answer, which is 5. So, I can say that in my array, this subarray
14:44was the largest subarray with sum is equal to
14:490. What is my process? In the process, I will take my sum every time. I will run the loop
14:54by j is equal to 0 to n. Sum is removed. See, sum exists in the map or not. If
14:59it exists, then I will compare the length to j-i. No, then I will add my sum to the
15:05map with my index j. This is why I will write code. My array has already given. What I will
15:10do is create a map. HashMap of Integer and Integer. New HashMap.
15:23I am going to store some and my index pair. Let's take two other variables. One is my sum which
15:30will be initialized with 0 and one is my length which will be initialized with 0. Now I have to
15:35run a loop for int j equals 0, j less than myarray.length j++
15:43What I will do every time? First I will remove sum. Sum will be plus or equal to my array
15:47of i. So i'th index or array of j. So I will remove sum until j'th index. First I will
15:53remove sum. Then I will remove sum of 15 and minus 2. Then I will remove sum of 15 and
15:58minus 2 and 2. Then I will remove sum from this way. Then I will check that if the sum
16:04exists in the map. So if my sum exists already, then what I will do in that case? I will
16:10compare length with j minus i.
16:12math dot max of length and j minus i j minus i kya ho jayega i ho jayega map dot
16:18get sum aur agar exist nahi karta to us case mein map ke anndar mein put kar dunggi my sum
16:25and my index which is equal to j last mein kya karunggi last mein print karwa dunggi apna answer largest
16:33sub array with sum as zero equals yah mein print karwa dunggi apne largest sub array ki length ko
16:44Let's save and now run. The answer has come. My largest subarray length is 5 to equal which is this
16:55subarray. That's enough.
16:57Now, we have solved this question in linear time. Because I have just run a loop inside the operations of
17:04the map and constant in average cases. So, the time complexity of this code is linear time. So, this is
17:11so much better than my brute force approach, which I have to put nested loops, which I have to deal
17:15with n-squared time complexity. Now, I have solved this question in an optimized way.