For those who have doubt about all negative elements: eg. [-2, -3, -1]: max=-2 (as initialised at beginning) sum = 0 + (-2) = -2 (negative so reset to sum=0) | also sum= -2 is not greater than max= -2 . So don't update max. sum = 0 + (-3) = -3 (negative so reset to sum=0) | also sum= -3 is not greater than max= -2 . So don't update max. sum = 0 + (-1) = -1 Here, sum = -1 which is greater than max= -2. So update max = -1 So we get "max = -1" at the end of interaction which is true. Try yourself with longer arrays.
@@faizuddin4032 no bro if all elements are negative the sum will be negative but she applied a condition that whenever the sum becomes negative we mark it as zero as we will not count it
So here mathematics comes into picture. For negative number the lagest would be just a single number. For example if array= [-2,-3,-1] here largest sum would be -1, because -1 is the only largest number here!!
but we are setting (sum = 0) if the sum is negative then how it take negative elements?? According the given code : i=0 where a[0] = -2 sum = -2 maxsum = -2 then sum = 0 i = 1 where a[1] = -3 sum = -3 maxsum = -2 then sum = 0 i = 2 where a[2] = -1 sum = -1 maxsum = -2 sum = 0 So were are getting -2 as maxsum!@@TechnosageLearning
Before u set the value of sum to 0 u check in the if condition whether the sum > max_sum or not that's where the max_sum gets updated to -1 coz at the end u return max_sum only.@@gowthamganesh2974
For those who have doubt about all negative elements:
eg. [-2, -3, -1]:
max=-2 (as initialised at beginning)
sum = 0 + (-2)
= -2 (negative so reset to sum=0) | also sum= -2 is not greater than max= -2 . So don't update max.
sum = 0 + (-3)
= -3 (negative so reset to sum=0) | also sum= -3 is not greater than max= -2 . So don't update max.
sum = 0 + (-1)
= -1 Here, sum = -1 which is greater than max= -2. So update max = -1
So we get "max = -1" at the end of interaction which is true. Try yourself with longer arrays.
What should happen if all elements in the array are negative?
If all elements are negative then the sum will also be negative number
Bro 1st complete ur 5th standard 😂
@@saarza9991 aapke pita ji jadge h nhi n to jadzment ku de rhe ho bhai
@@faizuddin4032 no bro if all elements are negative the sum will be negative but she applied a condition that whenever the sum becomes negative we mark it as zero as we will not count it
@@nitikarajpoot9869 yar itni funny baatein kaise kr leti hain ladkiyan
Waiting for FST 7 & ArthurJones / Mike Menzter's Heavy duty training style master !
Mam. how can we get the sub array with max sum
Thanks mam
wow!
brute force koi share kar do
Doesn't work when every element is negative. Bruhhhhh!!!
So here mathematics comes into picture.
For negative number the lagest would be just a single number.
For example if array= [-2,-3,-1]
here largest sum would be -1, because -1 is the only largest number here!!
but we are setting (sum = 0) if the sum is negative then how it take negative elements??
According the given code :
i=0 where a[0] = -2
sum = -2
maxsum = -2
then sum = 0
i = 1 where a[1] = -3
sum = -3
maxsum = -2
then sum = 0
i = 2 where a[2] = -1
sum = -1
maxsum = -2
sum = 0
So were are getting -2 as maxsum!@@TechnosageLearning
Before u set the value of sum to 0 u check in the if condition whether the sum > max_sum or not that's where the max_sum gets updated to -1 coz at the end u return max_sum only.@@gowthamganesh2974
Before u update the sum variable u check if sum > max_sum thats where the variable gets updated.@@gowthamganesh2974
@@gowthamganesh2974 you are wrong here. At i = 2, Max sum will be -1 because ( -1 > -2) hence maxSum = -1
xavier