I would initialize the Max_ sum with 0. If all elements of the array are negative, the correct answer would be the empty subarray with a sum of 0. If you initialize at -infinity, you get a negative sum as a solution in that case
You said that you wanted to get the subarray with the maximum sum, but you got the maximum sum, not the subarray. Either you misstated the problem or you botched the solution.
You say "We want to find the array that has the maximum sum", yet you are not returning the max sum subarray, you are returning the max sum number.
The key is to initialize `Max_sum` to the smallest negative number if deal with signed numbers or zero if deal with unsigned numbers; update `Max_Sum` to the first element after comparison, and set `Cur_Sum` to zero. Then move on to 2nd element and so on. But the algorithm in the vid seems to sum up the max sum. So, one would need to track the indices of where the max sum occurs for the sub-array found.
I prefer this for an interview than sticking a <div> to top while scrolling!
but not why [-2,7,4] ?
what if your max sum is negative?
I was procrastinating to search this, I got this in my feedπ
Luv ur shorts just to point and a lot of info πππ
I love these. Your explanations are very accurate and simple
Alright thatβs it Iβm subscribing
Erm ... I might be missing something here but for an array of [-2, 7, -3, 4] the sub-array with the maximum sum is [7, 4]. There is no requirement for a sub-array to have 1 less element than the original array nor does it have have to retain element order/position
-2 +7 +4 = 9 ... so sum of consecutive numbers? Why add 3 numbers? Clearly i am not understanding
is this a language barrier ? cuz a "subarray" for me is just a literal sub array derived from the original array, no matter the element selection order, so for me the max subarray would be -2, 7 and 4
May be a noob question. But couldn't you sort the array and sum the 3 largest numbers? Or would that just have worse timing?
Everyone shit talking in the comments saying this isn't the maximum subarray clearly haven't done their research. The Maximum Subarray problem isn't a problem that asks you to make a new subarray that results in the largest sum, it's a problem asking for the biggest SUM you can produce from a single contiguous subarray
indices of the chosen nos. must be saved to to know which nos. add up to maximum. I see it is missing
I work in development for 5 years, and still don't understand why companies still do this questions π π
Thankyou so much greg the way you explaining problem its becoming easy to understand we love it π
@GregHogg