Scala Programming: Find contiguous subarray within a given array of integers which has the largest sum
Write a Scala program to find contiguous subarray within a given array of integers which has the largest sum.
In computer science, the maximum sum subarray problem is the task of finding a contiguous subarray with the largest sum, within a given one-dimensional array A[1...n] of numbers. Formally, the task is to find indices and with, such that the sum is as large as possible.
Example:
Input:
int[] A = {1, 2, -3, -4, 0, 6, 7, 8, 9}
Output:
The largest sum of contiguous sub-array: 30
Sample Solution:
Scala Code:
Sample Output:
Original array: 1, 2, -3, -4, 0, 6, 7, 8, 9, The largest sum of contiguous sub-array: 30
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to find maximum difference between two elements in a given array of integers such that smaller element appears before larger element.
Next: Write a Scala program to find minimum subarray sum of specified size in a given array of integers.
What is the difficulty level of this exercise?