Skip to content

Instantly share code, notes, and snippets.

@1kohei1
Last active December 13, 2015 18:54
Show Gist options
  • Save 1kohei1/bef3d3056eef0ea1683a to your computer and use it in GitHub Desktop.
Save 1kohei1/bef3d3056eef0ea1683a to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Playground2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] nums = new int[n];
int max = 0;
int sumSoFar = 0;
int startIndex = 0;
int endIndex = 0;
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
if (sumSoFar == 0 && sumSoFar + nums[i] > 0) {
startIndex = i;
}
sumSoFar += nums[i];
if (sumSoFar < 0) {
sumSoFar = 0;
}
if (max < sumSoFar) {
max = sumSoFar;
endIndex = i;
}
}
System.out.println(max);
for (int i = startIndex; i <= endIndex; i++) {
System.out.printf("%d ", nums[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment