We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2b27f3a commit aa73f79Copy full SHA for aa73f79
src/me/ramswaroop/arrays/LargestSumContiguousSubArray.java
@@ -20,9 +20,9 @@ public static int getLargestSumOfContiguousSubArray(int[] a) {
20
int maxSum = 0, maxSumTillIndex = 0;
21
22
for (int i = 0; i < a.length; i++) {
23
- maxSumTillIndex += a[i];
+ maxSumTillIndex += a[i]; // keep on adding elements
24
if (maxSumTillIndex < 0) {
25
- maxSumTillIndex = 0;
+ maxSumTillIndex = 0; // once the sum is less than 0 restart adding elements from next index
26
} else if (maxSumTillIndex > maxSum) {
27
maxSum = maxSumTillIndex;
28
}
0 commit comments