Skip to content

Commit 54f23d8

Browse files
author
duaraghav8@gmail
committed
Maximum Subarray Sum
1 parent 8dbe4a9 commit 54f23d8

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

algorithm/etc/dp/desc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"fibonacci": "Fibonacci Sequence",
1212
"sliding_window": "Finding the largest sum of three contiguous number",
1313
"max_sum_path": "Finding the maximum sum in a path from (0, 0) to (N-1, M-1) when can only move to right or down",
14-
"longest_increasing_subsequence": "Find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order"
14+
"longest_increasing_subsequence": "Find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order",
15+
"max_subarray": "Find the sum of the maximum Subarray in the given Array"
1516
}
1617
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var maxSubarraySum = (function maxSubarray (array) {
2+
var maxSoFar = 0,
3+
maxEndingHere = 0;
4+
5+
tracer._print ('Initializing maxSoFar = 0 & maxEndingHere = 0');
6+
7+
for (var i = 0; i < array.length; i++) {
8+
tracer._select (i);
9+
tracer._print (maxEndingHere + ' + ' + array [i]);
10+
maxEndingHere += array [i];
11+
tracer._print ('=> ' + maxEndingHere);
12+
13+
if (maxEndingHere < 0) {
14+
tracer._print ('maxEndingHere is negative, set to 0');
15+
maxEndingHere = 0;
16+
}
17+
18+
if (maxSoFar < maxEndingHere) {
19+
tracer._print ('maxSoFar < maxEndingHere, setting maxSoFar to maxEndingHere (' + maxEndingHere + ')');
20+
maxSoFar = maxEndingHere;
21+
}
22+
23+
tracer._deselect (i);
24+
}
25+
26+
return maxSoFar;
27+
}) (D);
28+
29+
tracer._print ('Maximum Subarray\'s Sum is: ' + maxSubarraySum);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var tracer = new Array1DTracer ();
2+
var D = [-2, -3, 4, -1, -2, 1, 5, -3];
3+
4+
tracer._setData (D);

0 commit comments

Comments
 (0)