• 2 days ago
Transcript
00:00Next, just the question we have talked about is subarray sum equal to k. Last question we have talked about
00:06is my subarray, which is the sum of my desirable value, which is the target value, which is equal to
00:130, which is the length of this subarray. This time, my target is the k value, which is not the
00:22length, which is the count. How many subarrays are the sum of k?
00:29If I have an example of my given array, I have elements 1, 2 and 3 and k value are
00:353 equal. Now I have to return the number of such sub-arrays which sum is equal to this k
00:40value which is equal to 3. I have an answer that 2 is equal because there are two sub-arrays
00:45exist. One sub-array is 1 and 2 which sum is 3 equal and the other sub-array is 3
00:52which is also the sum.
00:54I have equal to 3k equal. So in the answer, I will print 2 to for this k is equal
01:00to 3. Now the approach will remain the same. The approach will remain the same as we have seen last
01:04time. Whenever we talk about subarrays, arrays, then we always remember that if I take sum of 0 to j
01:17and sum of 0 to i, then sum of
01:24i plus 1, j, this minus is equal to this, this minus is equal to this, this minus is equal
01:32to this, this minus is equal to this,
01:45which all the index will be the sum that I will minus the sum. This is the basic fund which
01:52we have to use to solve this question. I can derive this same property from this same property. If I
01:59have 0 from J to this sum out of sum and then I have sum out of 0 from I
02:06minus 1, then I can say that my sum is i from J.
02:11It will be like this. It will simply replace i with i-1 everywhere. So here it will be equal
02:17to 0. Here i-1 plus 1 will be equal to i-1. So it will be sum of i
02:23to j. So we are going to use basic of this property to calculate number of sub-arrays which sum
02:30comes to k. Now what will be my approach? The approach will be the same as I have to take
02:35a map. My hash map will be.
02:40which I store in which I will store in which I will store in which I will store in which
02:44I will store in which I will store in which I will store in which I will store in which
02:48I will store in which I will store in which I will store in which I will store in which
02:54I will store in which I will store in which I will store in which I will
03:16Now let's talk about a basic thing, if I have left the sum of j
03:20And I have sum of i minus. So I want to calculate sum of i to j. I want to
03:27calculate sum of i to j. I want to calculate sum of i to j. I want to calculate sum
03:28of i to j. I want to calculate sum of i to j to j to j to j to
03:32j to j to j to j to j to j to j to j to j to j to
03:34j to j to j to j to j to j to j to j to j to j to
03:38j to j to j to j to j to j to j to j to j to j to
03:39j to j to j
03:48third class class class class class class class class class class class class
03:55Let's assume that I used a loop in which the value of j is 0, then j is 1, then
04:02j is 2. And every time I calculate the sum of j. What do I know? I know that if
04:09I have taken a map, then the sum of i already stored. So what do I have? I have sum
04:16of j. And if I minus k, then I have sum of i.
04:21with the same formula. So, I will check every time what my map exists in the sum of j minus
04:26k exists. If the sum of j minus k exists, it will be sum of i k equal. So, it
04:34will already exist in the sum of j minus k. So, what do I have to search every time? I
04:42will run a loop and
04:46I want to search for value sum of j minus k equal because this value is equal to sum of
04:53i. And sum of i already store in map. Sum of j calculate this is my sum value. K value
04:59already given. So I have to check this value every time.
05:04So what do I have to do in my question? I have to search for sum j minus k where
05:09the sum of i is equal to the value. So I will use a map to solve this question. I
05:15will store the pairs in this map. The pair will store the pairs. The pair will store the pair with
05:20the count. This means the sum of the value will be how many times the count will be stored. So
05:26the sum will be my key in the map and the count will be my value.
05:30I will take two variables first. One is my sum variable and one is my answer variable. In the answer,
05:36I will store my count. The answer is zero and the sum is zero. What do I need to do?
05:42I need to run a loop that j is equal to 0 to n. I will check what my sum
05:49of j is minus k exists in the map. If this value exists, what do I need to do? In
05:57this case, whatever count is stored in this value.
06:00I will add it to the answer. Answer plus is equal to map.get sum-k
06:07Sum minus k, but whatever count store will be added in my answer. After doing this, I have to put
06:14my value in the map. Put my sum and increase the frequency by 1. Increase the frequency by 1. If
06:23the sum is first, I will increase the value by 1. If the sum is not first, I will add
06:28the value by 1. This will be my total process. Once I start, I am going to put here.
06:36sum of 0 and 1 sum of 0 and 1 sum of 0 and 1 sum of 0 and 1
06:40sum of 0 and 1 sum of 0 and 1 sum of 0 and 1 sum of 0 and 1
06:40sum of 0 and 1 sum of 0 and 1 sum of 0 and 1 sum of 0 and 1
06:41sum of 0 and 1 sum of 0 and 1 sum of 0 and 1 sum of 0 and 1
06:43sum of 0 and 1 sum of 0 and 1 sum of 0 and 1 sum of 0
07:00which is equal to 0. Now, 0 will not get in the map, if I have not added it. By
07:05default, I will add 0 sum in the map, which is 1. This shows that you have found a total
07:12sub array, which is k, which is the total sub array, which is equal to k.
07:19This was the case in which I had a sub-array with i minus. But I got a sub-array
07:25with which there is not a minus. The total sub-array value is k. That is also a valid answer,
07:30so I will add 1 to that. I will give an example of this. Let's take this sub-array with
07:37which the value is 1, 2 and 3.
07:393 k equal, 1, 2 and 3, I have k given 3, here to here till I have sum calculated,
07:45this sum is equal to 3, now 3, I will search 3 minus k, k value is 3 k equal,
07:53this is 3 minus 3 equal 0, I will search 0, I will not get 0, because we add sum
07:58in the map, but in sum, I will not add 0, so I will form all the sum,
08:06For this search, I already have to add a 0 to which I know that the whole sub array will
08:11make my k sum. So what I have done with this logic? In the beginning, I have added 0 and
08:161. Now I will go to my first index. First index is 10, my sum is 10. I have to
08:24search 10 minus k which is equal to minus 10.
08:28minus 10, then this value becomes equal to 20. If 20 already exists, it doesn't exist. If it doesn't exist,
08:36don't do anything. Simply add our sum to the map. So, my sum is my 10. I have added 10.
08:41I have counted as 1. After that, I will go to my next index, or 2. Now, I will check
08:47for 2. I will check for 2, then my sum will be equal to 12. What will I search for
08:5112? I will search for 12 minus k, which is equal to minus 10. This value becomes equal to 22.
08:57Now, 22 does my map exist or does not exist? If it does not, then I simply add the frequency
09:02for my sum. What is the sum? The sum is 12, which means the count is 1. Now, I will
09:07come to my next index.
09:09next index, so I have come to this value, which is minus 2. Minus 2 will be sum of 10.
09:14Who will search for minus 2? Minus 2 will search for sum of j which is 10, minus k, minus
09:20of minus 10. This value becomes equal to 20. I have to search for 20. Does 20 already exist? 20
09:27already exists. What will I do? Simply put into the sum of j.
09:31I want to add 10 to 10. I want to add 10 already. So, I will increase the frequency and
09:37I will do 2. So, wherever you already exist, the frequency will increase. If you don't do it, then add
09:42new to frequency 1. Now, I will come to my next index, which is minus 20. Minus 20, like my
09:48sum, what happened? 10 minus 20. This is equal to minus 10. I have to search for which I have
09:54to search. Minus of minus 10. This value becomes equal to 0.
10:00Does 0 already exist in the map? Yes, it exists. Which value exists in the map? It exists on 0.
10:06Does it exist with count? It exists with 1. So I will add 1 to my answer. So 1 is
10:13added in the answer and the value is equal to 1. And with 7 I will add my sum to
10:17the map. So my count will be equal to minus 10. Now I will come to the next index.
10:24next index, so I have to go to 10. What is my sum? My sum is minus 10 plus 10
10:30which is equal to 0. I have to find out who to do? I have to find out that 0
10:35minus k, which is equal to 10. I have to find out that 10 already exists in the map. What
10:43is the count of 10? 10 is 2. I will add 2 to the answer. 1 plus 2 is equal
10:48to 3.
10:50I will increase the frequency for 0. Finally, when my loop is finished, I will get the last answer. I
11:00will get the last answer. I will get the last answer. I will get the last answer. I will get
11:02the last answer. I will get the last answer. I will get the last answer. I will get the last
11:03answer. I will get the last answer. I will get the last answer. I will get the last answer. I
11:13will get the last answer.
11:1810k equal. And another 3 sub array which is sum minus 10k equal which is my this sub array. So,
11:25these are 3 sub array which exist, which is my sum minus 10k equal. Process is what I am going
11:31to do. Process is what I am going to do. Process is what I am going to do. Process is
11:35what I am going to do. Process is what I am going to do. Process is what I am going
11:37to do. Process is what I am going to do. Process is what I am going to do. Process is
11:43what I am going to do.
11:43sum to 0 and count to 1. This is necessary so that if my total array of sum kk equals
11:49or complete array of sum kk equals, then I need this case. So, I have to insert it first. Then
11:56I will run a loop from 0 to n. I will check every time if sum j-k value exists.
12:01If it exists, then it will come in the map. If it happens, then whatever count was.
12:09Add it in the answer. If it is not, then what did I do? Put the sum with a frequency
12:16plus 1. If you exist, I will increase in frequency with 1 as its frequency. I will store it with
12:231 as its frequency. For this, I am going to write code. What do I do? I have made an
12:27area with my value k. Now I will take a hash map which will store integer integer key value pairs.
12:37I will name map is equal to
12:39new hash map. Hash map का काम है track करना मेरे pairs को pairs के sum and count के pairs
12:48को. अब शुरुवात में ही map के अंदर हम add कर देते हैं 0 और 1 को because this is
12:54very essential. अब sum को initialize करूँगी 0 के साथ and answer को initialize करूँगी 0 के साथ. अब एक
13:04loop चला लूँगी for int i equal 0 i less than my array dot length i plus plus.
13:12What do I do every time? I have to check that sum of j minus i. This is actually a
13:17loop of j. You can use a variable for understanding. I am using j for understanding. I have to search
13:24for sum of j minus k. So if my map.contains key, sum, I will add in sum plus equal
13:37to my array of i. This is sum of j.
13:43This is my sum of j. I have to search for sum of j minus k. If it exists, what
13:50do I do in this case? I will add its count. Map.get sum minus k. After this step, I
13:58will put my sum in the map.
14:02So I have put the sum and what is the value for it? If the frequency counts already exist and
14:08the value is first, then add it to the value. No, then start with 1. So I have a function
14:13called get or default value. I am going to use map.get or default value. In which I will pass
14:20in the start of my key is sum. If it exists first, then the sum of count is not. Then
14:27the default value is 0. And then I will add 1.
14:32So this will be my process. I can also write it with FL. If you feel FL more comfortable, you
14:36can also write it with it. Otherwise, it will be better that we write it with this way, because it
14:40is optimized, it is small, I understand quickly. What did I do with this? If it exists before, I have
14:47increased the count by 1. No, the count is 0 and now I have added it, plus 1. Now what
14:52do I do with last? I have to print my answer. So I will print the answer. Let's do this,
14:57save and run.
15:02I have the answer that is equal to 3. How much time did I have? This process, this code is
15:09very small, but we have a lot of logic. That is why logic is more important. How many lines of
15:15code is not important. Our logic needs to be very clear. And when the logical logic is very clear, they
15:22know how to minimize our code in little lines. How do we get better and better answers? Now talk about
15:30time complexity here.
15:32This is my constant time. This is my loop for big of n map. So this is a linear complexity
15:40question. If we brute force it, it seems like n2 time. Now I have done it from linear, so I
15:46have used the hash map data structure. Yes, I have used the hash map, so I had to give more
15:50space because I have stored pairs. But my time complexity has decreased. And usually the main priority in questions is
15:58my time complexity.
15:59So now we have asked a lot about hash sets and maps. We have done a lot of concepts in
16:05maps and sets and hashing. And I hope that we have come to understand very well. Because hashing is a
16:11very important concept in programming. Now it is not that we are studying the concept of hashing in this chapter.
16:16We will study dynamic programming, graphs. So we will have a lot of questions in which we are using hashing,
16:23maps, sets, to count frequency, to track sub-array sum.
16:29So we will use these data structures here. We will use practice questions in the future. We have already used
16:35hash maps in trees when we asked the top view of a binary tree.
16:41So you will know how to use two data structures and solve a single question. So when you have a
16:47coding test or a interview question, you don't need to use one data structure. You can use multiple DS users.
16:54So we have to open up our minds, which are our classical questions. We have to give our tips that
16:59we have logic in this way. After that, the new questions will be solved by ourselves. So we have to
17:04practice properly, keep our logic properly.