2D Array Max Subarray Sum
Write a method maxSubarraySum
that, given a non-rectangular two-dimensional int
array, returns the sum of the
subarray that sums to the largest value.
So given the following array, with each subarray on a separate line:
You would return 7.
assert
that the passed array is not null
.
However, if the passed array is not null
it will contain no empty subarrays.
One hint for this problem is that you may need both an int
variable to store the max and a boolean
variable
to record whether the maximum value has been initialized.
Once you have summed each subarray, check whether either your boolean
value is false
or the sum is larger than
the largest you've seen so far.
After you check the sum of the first subarray, set your boolean
value to true
.
Another approach is to use the counter that you use to proceed through each subarray to determine whether you have
initialized the max value.