2019-05-09 19:32:54 +08:00
|
|
|
|
package DynamicProgramming;
|
|
|
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-24 18:23:28 +08:00
|
|
|
|
* Program to implement Kadane’s Algorithm to calculate maximum contiguous subarray sum of an array
|
2019-05-09 19:32:54 +08:00
|
|
|
|
* Time Complexity: O(n)
|
|
|
|
|
*
|
|
|
|
|
* @author Nishita Aggarwal
|
|
|
|
|
*/
|
|
|
|
|
public class KadaneAlgorithm {
|
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
|
/**
|
|
|
|
|
* This method implements Kadane's Algorithm
|
|
|
|
|
*
|
|
|
|
|
* @param arr The input array
|
|
|
|
|
* @return The maximum contiguous subarray sum of the array
|
|
|
|
|
*/
|
|
|
|
|
static int largestContiguousSum(int arr[]) {
|
|
|
|
|
int i, len = arr.length, cursum = 0, maxsum = Integer.MIN_VALUE;
|
|
|
|
|
if (len == 0) // empty array
|
|
|
|
|
return 0;
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
cursum += arr[i];
|
|
|
|
|
if (cursum > maxsum) {
|
|
|
|
|
maxsum = cursum;
|
|
|
|
|
}
|
|
|
|
|
if (cursum <= 0) {
|
|
|
|
|
cursum = 0;
|
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
|
return maxsum;
|
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
|
/**
|
|
|
|
|
* Main method
|
|
|
|
|
*
|
|
|
|
|
* @param args Command line arguments
|
|
|
|
|
*/
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
|
int n, arr[], i;
|
|
|
|
|
n = sc.nextInt();
|
|
|
|
|
arr = new int[n];
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
|
arr[i] = sc.nextInt();
|
2019-05-09 19:32:54 +08:00
|
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
|
int maxContSum = largestContiguousSum(arr);
|
|
|
|
|
System.out.println(maxContSum);
|
|
|
|
|
sc.close();
|
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
}
|