Finds the maximum sum of the elements of a subarray in a given array
using the divide and conquer algorithm by Bentley, Jon (1984).
For example, for the sequence of values -2, 1, -3, 4, -1, 2, 1, -5, 4
the contiguous subarray with the largest sum is 4, -1, 2, 1, with sum 6.
Time complexity: O(N log N).
Time complexity: O(N log N).
Parameters:
Name | Type | Description |
---|---|---|
array |
Array
|
Input array. |
Returns:
- Type:
-
Number
Maximum sum of the elements of a subarray.
Example
var max = require('path-to-algorithms/src/searching/'+
'maximum-subarray-divide-and-conquer').maxSubarray;
console.log(max([-2, 1, -3, 4, -1, 2, 1, -5, 4])); // 6