A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Maximum Subarray in Java page! Here, you'll find the source code for this program as well as a description of how the program works.
import java.util.Arrays;
public class MaximumSubarray {
public static void main(String[] args) {
int[] input = parse(args);
System.out.println(maximumSubarraySum(input));
}
private static void usage() {
System.err.println("Usage: Please provide a list of integers in the format: \"1, 2, 3, 4, 5\"");
System.exit(1);
}
private static int[] parse(String[] args) {
if (args.length != 1 || args[0] == null || args[0].isBlank()) {
usage();
}
try {
int[] values = Arrays.stream(args[0].split(","))
.map(String::trim)
.mapToInt(Integer::parseInt)
.toArray();
if (values.length == 0) {
usage();
}
return values;
} catch (NumberFormatException e) {
usage();
return new int[0]; // unreachable
}
}
private static long maximumSubarraySum(int[] numbers) {
if (numbers.length == 0) {
return 0;
}
int currentSum = numbers[0];
int maxSum = numbers[0];
for (int i = 1; i < numbers.length; i++) {
int number = numbers[i];
currentSum = Math.max(number, currentSum + number);
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
}
Maximum Subarray in Java was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.