 What's up guys, I'm Michael and welcome to my YouTube channel today We're gonna do a maximum sub-array problem the leak code next challenge of a 30-day leak code challenge You're given an integer array nums and find the contiguous summer sub-array Which has at least one element which has the largest sum and return of some so this is the array The output is six The explanation is that this sub-array has a largest sum six, okay? So a sub-array when they mean contiguous sub-array. It means that it's like it's an array That is part of the large bigger array like a smaller array. That's part of the bigger ray and it's contiguous So that means that the numbers are Next to each other so negative two one negative three four That would be a contiguous sub-array this negative one Negative two one negative three is another contiguous sub-array. All right, so we're not picking random elements from the array We're not picking it a negative two the negative five and then four, okay? We're not doing that so what we're doing is we're trying to find the contiguous sub-array that has the largest sum That's we're trying to do okay We're trying to find the contiguous sub-array that has the largest sum and return it some okay so to do this you actually just run this algorithm called Cadane's algorithm and I'm gonna briefly explain how this works while I'm typing the code up But maybe I'll have a more in-depth video later on So yeah, how it works is that we're gonna start because okay, so we need we we know that the We know that We need to return the max sum right so I'm gonna have a variable to call the max sum and Then I'm gonna have another variable called the current sum and These variables are gonna be initialized with the first value. Okay, so what am I gonna do? What am I gonna do? So we know that When we're looping through here, right? We need to find every single sum for every single sub-array contiguous sub-array, so here What am I gonna do? I'm gonna try to I'm gonna loop through every through every value here I'm gonna find the current contiguous sum up to this point So at this point, I'm gonna find the current sum negative two one negative three four or the maximum contiguous sum so the then the max max sum that is possible at this current point and I'm going to Compare that and get the maximum sum at this continue current point So to do that, I'm gonna pretty much sum up the values beforehand up to this point and I have to make sure that Check am I resetting this sum? Okay, so if at the next point, let's say I hit a negative one And I don't want this negative one to be inside my sum that I currently calculated. I'm gonna start a new sum From that point, so I'm gonna if I'm if uh, if negative one is not there I'm gonna start a new sum after the point. I don't add negative one Okay, so how am I gonna do that first? I'm going to maintain two variables called max sum and then current sum I'm gonna loop through the end of the ray Because we already started our max sum at zero, right and current sum those have values zero So I'm gonna start from one all right one to the end and what am I gonna do? I'm going to Get the current sum and it's gonna be the max value of nums at I plus my current sum But I also have to check if I'm gonna reset it or not So if I'm gonna reset this That means that the I don't want to add the next value into my current sum, right? So if I'm gonna reset it, I'm gonna do nums at I Okay, so this is going to pretty much check find the maximum value that that I'm currently at so if My nums at I is larger like if my current value that I'm currently on like four if it's larger than Yeah at four let's say I'm at four if it's larger than the Are at my already previous some that I already added up currently then I'll reset it Okay, I'll reset my current sum to be starting from new some starting from four So that's what I'm gonna do here. I do a current sum is going to equal the maximum value of the nums nums at I which is the The current value I'm on plus the current sum that previously that I already added up I'm gonna find the maximum of this and the new value that I'm gonna start at because if it's if the new value That I'm gonna add is larger than all this stuff Then I'm gonna reset my current some right to be the new value that I'm starting at all right Then I'm gonna find max some which is gonna just gonna be the maximum of my current max some and current some After that, I'm just going to return max some Okay Submit and it got accepted. So this is how you do it. This is a brief overview of Cadane's algorithm I'm gonna like go over it more in depth with an example in a next video, but this is just like a brief Brief run overview of Cadane's algorithm. All right, I keep making these videos are too long But I don't I don't want to make it like too long. So yeah, this very brief video I hope you guys enjoyed this video maximum sub array ray com subscribe. I'll check you guys later. Peace