Maximum Subarray | Kadane's Algorithm | Leetcode 53
ฝัง
- เผยแพร่เมื่อ 6 ก.ย. 2024
- Maximum Subarray | Kadane's Algorithm
Leetcode problem number 53
Time Complexity: O(n)
Space Complexity: O(1)
JAVA interview programming playlist:
• Interview Programming ...
#java #interviewquestions #leetcode
Please continue this series. Also, please explain the time and space complexity concepts in this series. It will be helpful for FAANG interview preparation.
Sure ,Will make a video on time and space complexity soon
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 !
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