💸 Visit https://www.educative.io/anuj to avail discount on all courses on Educative! 💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
📚 Complete DSA Playlist: • DSA-One Course - The Complete Data St…
Complete Android Development Playlist: • Android Development Tutorial for Begi…
Hashtags: #anujbhaiya #dsaone #kadane #algorithm
Ignore these tags:
kadane’s algorithm kadanes algorithm kadane kadane algorithm algorithm maximum sum subarray kadane algorithm kadane’s algorithms kadanes algorithm maximum sum subarray kadanes algorithm to maximum sum subarray kadane’s algoritm kadane’s algorithm maximum sum subarray kadane algorithm java kadanes kadanes algorithm java data structure algorithms kadanes algorithm kadane’s algorithm in c kadane’s algorithm java kadanes algorithm explained kadane’s algorithm kadane algorithm kadanes algorithm algorithm kadane kadane algorithm c++ kadane’s kadane algorithm java algorithms kadanes algorithm maximum sum subarray kadanes algorithm to maximum sum subarray kadane’s algorithm maximum sum subarray kadane’s algorithm in c maximum sum subarray kadane algorithm kadane algorithm 2d kadane algorithm dp kadane algorithm complexity kadane’s algorithm to maximum sum subarray problem kadane’s algorithm kadane algorithm kadane’s algo v.v.v.v.v imp largest sum contiguous subarray kadanes algorithm maximum subarray maximum subarray problem maximum sum subarray maximum subarray sum anuj bhaiya maximum subarray leetcode kadane’s algo kadens algorithm kadane’s algorithm subarray kadane kadane algo kadanes algo maximum sub array maximum product subarray max subarray kadane’s algorithm in hindi max subarray sum find largest sum contiguous subarray algorithm maximum circular subarray sum max sum subarray kandane algorithm leetcode 53 max sum contiguous subarray subarray with given sum max sub array maximum subarray dsa kadane algorithm in hindi split array largest sum max product subarray longest and subarray subarray sum dsa one max circular subarray sum maximum frequent subarray sum continuous subarray sum subarray sum equals k algorithms in java anuj bhaiya dsa array algorithms leetcode maximum subarray maximum sum triplet algorithms greedy algorithm anuj array algorithm find subsequence of length k with the largest sum kadane’s algorithm hindi maximum sum rectangle aditya verma dynamic programming contiguous subarray kadane’s algorithm jenny maximum beauty subarray maximum sub array sum maximum sum circular subarray subset sum problem two pointer algorithm course k-th largest sum contiguous subarray longest subarray max subarray problem maximum frequent subarray sum codechef solution maximum subarray sum cases anuj bhaiya java apna college contiguous array dsa course find largest sum contiguous subarray [v. imp] given an array arr[] of n integers. find the contiguous sub-array(containing at least one number) which has the maximum sum and return its sum. kadens algo longest and subarray codechef maximize the sum hackerearth maximum contiguous sub-array problem partition array for maximum sum algorithm in java kadane’s algorithm kadane algorithm kadane’s algo v.v.v.v.v imp largest sum contiguous subarray maximum subarray kadanes algorithm maximum subarray problem maximum sum subarray maximum subarray leetcode maximum subarray sum anuj bhaiya kadane’s algo kadens algorithm kadane’s algorithm subarray kadane algo kadane kadanes algo maximum sub array max subarray maximum product subarray kadane’s algorithm in hindi max subarray sum find largest sum contiguous subarray algorithm maximum circular subarray sum 53. maximum subarray leetcode 53 max sum subarray kandane algorithm max sum contiguous subarray subarray with given sum max sub array dsa split array largest sum kadane algorithm in hindi subarray sum max product subarray longest and subarray dsa one leetcode maximum subarray max circular subarray sum maximum frequent subarray sum continuous subarray sum algorithms in java greedy algorithm subarray sum equals k anuj bhaiya dsa array algorithms
Content
0 -> Hello whatsup guys, Anuj here and welcome to the DSA 1 course and today we are going to talk about arrays
4.09 -> Today we will move ahead in arrays, and discuss one more important question in array that is maximum sum subarray
10.2 -> And there is an important algorithm in this, which is Kadane's Algorithm
12.919 -> This question and this algorithm is already been asked in many big companies, like Paypal, Amazon, Microsoft have asked them
21.109 -> There is an important concept in this, and the Kadane's Algorithm is very important
24.92 -> We are going to understand this, what is the question?
27.59 -> Question is maximum sum subarray, means you are given an array and you have to find a small part of that array, a subarray
35.384 -> Whose sum is maximum
37.553 -> Suppose this is your array, and it has both positive and negative element, from this you have to find a small subarray, whose sum is maximum
47.32 -> So over here if we see, we will get this one, 4,6,-3 and 4, sum of these is the maximum, we will add and see, it will be 11
64.827 -> Basically, this is our question and there are multiple techniques to solve this, already you might be getting multiple techniques in your brains
69.776 -> But the most efficient technique is Kadane's algorithm
72.956 -> We are going to see it
74.985 -> First of all brute force solution, to do any question brute force solution should come into your mind first
79.946 -> What could be the brute force for this? I am able to see over here that I have a sub array, and whenever there is a question of sub array
85.509 -> So to find the sub array, your brute force should be to find all the sub array
93.701 -> How many arrays can be formed into this, I will see all the subarrays
98.286 -> And after that I will calculate the sum of all
100.801 -> And the subarray with maximum sum, will be my answer
103.325 -> In this array if want to traverse all the sub arrays then one technique can be we will use 2 for loops
110.408 -> What it will be? we will start from here, so first subarray will be - 5 to 5
114.898 -> Next will be -5 to 4, next will be -5 to 6,
117.989 -> -5 to -3, -5 to 4, and -5 to -1
121.564 -> So, we have seen all the sub array starting from -5
125.131 -> Now, we will see sub array that are starting form 4
127.257 -> 4 to 4, 4 to 6, 4 to -3, 4 to 4, 4 to -4
132.119 -> So now we have seen all the sub array starting from 4
134.822 -> Now, we will jump to 6
136.234 -> Now we see all the subarrays starting from 6
138.556 -> 6 to 6, 6 to -3, we will move on like this
141.6 -> We will see all the sub arrays like this
143.625 -> And our answer will be this sub array 4 to 4, fine
146.805 -> And we will return that, code is going to be very simple
148.723 -> Basically, it will be like this, first for(int i=0; i
155.8 -> and one more nested for loop will be in it
162.583 -> for( int j=i; j
166.159 -> And by doing like this we can traverse all the subarrays
168.624 -> By doing like this, we can go through all the subarrays
173.174 -> We will go on calculating sum, int sum, and we will make maxsum=-infinity
186.386 -> And when ever we will feel that this sum is greater than maxsum, then we will put it into maxsum
191.251 -> And by doing so, we will finally return maxsum, this is one way
198.585 -> But over here can you see, that the time complexity is O(n2), an interviewer will say this is not a good time complexity
211.088 -> And try to Optimize this
213.682 -> So you will try to optimize this, if you remember the last question
219.27 -> What we had done into that?
220.168 -> We had sorted the array, but I had told you that you should sort only when order does not matter
226.941 -> Over here as soon as you will sort this, your order will be changed
230.996 -> Now your array will be -3,-5,-1, 4,4,6 it will be like this
237.125 -> So, you had to find subarray, but you changed the order so now there is no question only
242.768 -> So, now you cannot find the subarray, so sorting is out of picture, we cannot do sorting in this
247.347 -> Sorting cannot be done, so what ever you were thinking related to sorting, binary search and all, now leave all that, so it will not be n(log n)
255.284 -> So this means, over here we can use space, we can try to solve using space
262.222 -> So think it once, can this thing be done using space?
265.781 -> Or you have to think of something else
268.151 -> Think, so if you people might have thought, there is no use of using space also
274.179 -> Because even if you will use space, you will have to see all the subarrays, so the time complexity will be O(n2)
282.369 -> So by using space also this will not be solved
284.476 -> Because we will have to see all the sub array, so what is the benefit
287.498 -> We will not use space, but it is still said that we can optimize this, then we will have to think one more logic, with which we can solve this question
297.796 -> What was the question, question was maximum sum subarray
301.506 -> Means, you have to find such subarray, whose sum is greatest
305.244 -> Suppose, we will start from a smaller problem
308.306 -> First of all in the array we have just 1, so the answer is 1
313.448 -> Suppose next we have 2 in the array, then the answer is 1+2=3
320.962 -> Suppose array contains 1,2,3, so answer will be 1+2+3=6
326.85 -> So can you understand 1 thing from here, that if all the elements are positive then we can do sum of all and that is our answer
335.245 -> If you keep on bringing more positive elements, so we will just do sum of all and that only will be our answer
342.597 -> ok, but the problem comes when we add a negative element in this
348.439 -> Suppose, after 1, I have -2, then we will have to think with negative elements
355.899 -> So now there are 2 elements in this, 1 and -2
360.248 -> So will I include -2, no I will not include
362.943 -> So my answer will be 1
364.05 -> Fine, 1 is the answer
366.287 -> Now suppose, we after 1, -2,we get one more 1
369.838 -> so now what will be the answer, so now can I try to make the whole sub array
376.562 -> Again it is subarray and not subsequence which means you have to include all the elements
379.794 -> So can we get answer from these 3?, we will get the answer 0
383.744 -> And if I use any one element, any 1, then my answer will be 1
388.276 -> So I can say its sum maximum subarray is 1
390.04 -> Either take this or this
391.927 -> Means, I have not included this one
394.023 -> Ok, if I put 2 over here, then what will happen?
398.163 -> So -2 +2=0, an this is 1
401.486 -> So if I try to include everything it will be 1
406.1 -> If I skip this 1, then my answer will be 2
411.217 -> So, from here only we can see one more thing, that if I do 3,4 or 5 over here, suppose I am doing 3
417.804 -> Then I see that there is benefit in keeping this 3, because -2 is reducing the value of 1
423.33 -> due to this -2 I am not able to include this 1
427.278 -> This means, after this even if there is something else, suppose after 3 there is 2 or 1, suppose anything comes after 3
433.661 -> I know that if I want to include 3 then I will not include -2 and 1
438.677 -> Because due to -2 the value of 1 has reduced
441.86 -> If I try to include this part then I am taking -1, 1 + -2=-1
448.259 -> This part is giving me -1
449.587 -> So why will I include this part?
451.285 -> So over here we should think one thing that if we are going like this, we are going one by one, because we are told we have to solve in O(n)
458.114 -> So we are moving one by one in this, then we don't have to take this part, if its total sum is negative
463.156 -> If any part, any subarray's total sum is negative, then there is no benefit in including that part
469.744 -> If we can see the sum of this part is -1, then there is no benefit in including it
474.575 -> Because it will reduce overall sum
476.396 -> This part will always reduce the overall sum, so we will not include this
480.401 -> Now we will say, your work is done over here only
483.367 -> Maximum you can get is 1, other then that you cannot bring anything, so this part is finished
492.591 -> Now we can start the same story from here
495.135 -> Same story will start from here
496.576 -> Now we took 3+2=5, suppose we added an element -6, then we added 4
503.41 -> 3+2=5, now 5+(-6)=-1, so this part is giving me -1
512.514 -> And now I will come here at 4, should I include this whole part into this where I am getting 3 and 2
519.925 -> I will say no, I am getting 3 and 2, but over here -6 is also there, I am getting overall a negative number, so it will reduce my sum
525.641 -> At present I am getting 4 from here, but if I will include everything from here then I will get 4 + -1=3
531.107 -> There is no benefit so, again negative element is discarded, you said whatever maximum I can get from this part is 3+2=5
539.174 -> So, I will just take 5 from here and do my work, I will move ahead
544.255 -> Now you can start to do the processing with 4
545.69 -> So in this way you can move ahead
549.191 -> What's the basic trick? trick is you keep on moving ahead but as you see the sum is negative, overall sum is negative
555.97 -> You discard that part over there only
558.865 -> And start the story from next element
560.148 -> Start the story again from next element
562.175 -> Now again over here we have 4, it is possibile that there can be anything in it,it can be 1000, 100 anthing can be over here
567.221 -> Even -200, so same story will start from here also
573.546 -> Now we got, 4+100=104,
577.297 -> After 104 it is -200, so 104 is our answer
581.856 -> Why 104 will be the anser?
584.159 -> Why are you not inculdeing this part in 104?
586.584 -> Because if we will include this part then we will get, 3+2=5
561.05 -> Now again we have 4 over here, anything can be after this 1000,100 or -200
569.895 -> So, a new but same story will start from here
573.388 -> Again we got 4+100=104, then -200, so 104 is our answer
581.809 -> Why 104 is the answer?
584.143 -> Why are we not including this part?
586.206 -> Because if we will include this part then we will get 3+2=5 + -6=-1
591.923 -> So if we will try to include this part then we will get 103 instead of 104
596.611 -> There is no benefit in taking 103, we have benefit by taking 104
598.698 -> So we will discard this part
600.026 -> Because the overall sum is negative
602.505 -> So this is our Kadane's Algorithm, it is based on this thing only
606.35 -> This algorithm which I have told you over here, is Kadane's Algorithm and I will code it once
610.454 -> So you will understand it clearly
612.79 -> This basically will get solved in O(n), because you are traversing only 1 time in whole array
617.04 -> You have not used any space, so this will bring from O(n2) to O(n)
624.341 -> This is quite huge optimization to bring from O(n2) to O(n)
628.045 -> So this is quite good algorithm
629.681 -> I think you might have understood also
631.067 -> I will code this and show it to you
632.946 -> Ok so I have coded the same thing over here, this maxSumSubArray function which is taking an array
638.625 -> And its works is to return the maxsum being calculated from subarray
642.541 -> So I have made 2 variables, maxsum and cursum, which I had told you in the same logic
647.92 -> We will start here from, 0
650.727 -> And we will keep on moving forward, so cursum=cursum+a[i], cursum was initially 0, so 0+a[i]
660.208 -> Means the current element, I am taking this example [5,-4,-2,6,-1], so cursum =5
667.937 -> So cursum=5 and maxsum was 0
673.728 -> So we will check if cursum>maxsum, yes 5>0, then maxsum=cursum, so maxsum=5
684.071 -> And we will check if our cursum is negative? no our cursum is not negative, we will go in next iteration
689.827 -> We will add next element in cursum, so 5 + -4= 1, so cursum=1
696.302 -> We will check if cursum>maxsum, no, so we need not do anything, and cursum is also not negative, so we not do anything, we will go on next element
708.323 -> We will add next element in cursum, samething we will add in current
713.516 -> So -2 +1=-1, is this greater than maxsum, is this negative?yes this is negative
721.663 -> So if this is negative, then cursum=0, this part is giving overall negative sum so we will discard this part
730.744 -> And because this is coming negative, so cursum=0, so we are not going to get answer from this part, maximum which can come from this part is 5
737.808 -> So we have kept maximum with us but contribution of this part was till here only, we are not going to consider this part any further
745.25 -> We will move ahead, next is 6, we will add 6 in cursum, so cursum=6
750.61 -> So 6>5, so we will come here, is cursum>maxsum? yes, 6>5, so maxsum=6, and it is not negative so we will move forward,and we will see -1 over here
766.312 -> If we will include -1, then it will be 5, is this greater than maxsum?no. So in the end our maxsum will be 6, loop will be finished
776.4 -> We will return maxsum. we finally returned maxsum, I hope you all can see the last line
781.323 -> So in this way, Kadane's Algorithm works. This algorithm converts this solution into O(n)
788.551 -> One more thing, if all the elements are negative then you will have to solve this problem in a bit different way
792.948 -> I am giving you all this as task , that if all the elements are negative, then try to find the issue in this solution, if all the elements are negative then it will not give the answer
806.335 -> And try to correct it, it is not that big, if all the elements are negative then it will be solved in a different way
812.488 -> So you have do some tweaks in this and you will get it
815.768 -> So with this I will give you the link in description, so you can go and see there
822.423 -> With this only I will end this video. In this we have seen Kadane's algorithm
825.296 -> Which is a very important algorithm
826.972 -> Like this video, share and subscribe to the channel