File tree Expand file tree Collapse file tree 1 file changed +5
-5
lines changed
src/algorithms/sets/maximum-subarray Expand file tree Collapse file tree 1 file changed +5
-5
lines changed Original file line number Diff line number Diff line change @@ -18,10 +18,10 @@ export default function dpMaximumSubarray(inputArray) {
1818 let currentSum = 0 ;
1919
2020 // We need to keep track of the starting and ending indices that contributed to our maxSum
21- // so that we can return the actual subarray.
21+ // so that we can return the actual subarray. From the beginning let's assume that whole array
22+ // is contributing to maxSum.
2223 let maxStartIndex = 0 ;
23- let maxEndIndex = inputArray . length ;
24-
24+ let maxEndIndex = inputArray . length - 1 ;
2525 let currentStartIndex = 0 ;
2626
2727 inputArray . forEach ( ( currentNumber , currentIndex ) => {
@@ -31,7 +31,7 @@ export default function dpMaximumSubarray(inputArray) {
3131 if ( maxSum < currentSum ) {
3232 maxSum = currentSum ;
3333 maxStartIndex = currentStartIndex ;
34- maxEndIndex = currentIndex + 1 ;
34+ maxEndIndex = currentIndex ;
3535 }
3636
3737 // Reset currentSum and currentStartIndex if currentSum drops below 0.
@@ -41,5 +41,5 @@ export default function dpMaximumSubarray(inputArray) {
4141 }
4242 } ) ;
4343
44- return inputArray . slice ( maxStartIndex , maxEndIndex ) ;
44+ return inputArray . slice ( maxStartIndex , maxEndIndex + 1 ) ;
4545}
You can’t perform that action at this time.
0 commit comments