Problem -
I have used the cumulative approach for the following code in Java to find subarrays with maximum sum but I am not getting Correct Output.
Can anyone guide me how to correct this?
Solution I tried
int maxSum = 0;
int[] cumSum = new int[input.length];
cumSum[0] = input[0];
for (int i = 1; i < input.length; i++) {
cumSum[i] = cumSum[i - 1] + input[i];
}
//For each subArray Calculate Cumilative Sum
for (int i = 0; i < input.length; i++) {
int currentSum = 0;
for (int j = i; j < input.length; j++) {
if (i - 1 >= 0)
currentSum = cumSum[j] + cumSum[i - 1];
else
currentSum = cumSum[j];
System.out.println("Current Sum is " + currentSum);
maxSum = Math.max(cumSum[i], currentSum);
}
}
Input
1,2,3,4
Output
16