2017-02-16 23:51:56 -06:00
|
|
|
// Author: Rodney Shaghoulian
|
|
|
|
|
// Github: github.com/rshaghoulian
|
|
|
|
|
// HackerRank: hackerrank.com/rshaghoulian
|
|
|
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
2017-04-26 16:15:39 -07:00
|
|
|
// A subarray must be contiguous. There are O(n^2) contiguous subarrays.
|
2017-02-16 23:51:56 -06:00
|
|
|
|
2017-04-05 18:42:35 -05:00
|
|
|
// Time Complexity: O(n^2)
|
|
|
|
|
// Space Complexity: O(1)
|
|
|
|
|
public class Solution {
|
2017-02-16 23:51:56 -06:00
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Scanner scan = new Scanner(System.in);
|
2017-04-05 18:42:35 -05:00
|
|
|
int size = scan.nextInt();
|
2017-04-26 16:15:39 -07:00
|
|
|
int[] array = new int[size];
|
2017-02-16 23:51:56 -06:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
array[i] = scan.nextInt();
|
|
|
|
|
}
|
|
|
|
|
scan.close();
|
|
|
|
|
|
|
|
|
|
System.out.println(negativeSubarrays(array));
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-26 16:15:39 -07:00
|
|
|
private static int negativeSubarrays(int[] array) {
|
2017-02-16 23:51:56 -06:00
|
|
|
int count = 0;
|
|
|
|
|
for (int i = 0; i < array.length; i++) {
|
2017-04-26 16:15:39 -07:00
|
|
|
int sum = 0;
|
2017-02-16 23:51:56 -06:00
|
|
|
for (int j = i; j < array.length; j++) {
|
|
|
|
|
sum += array[j];
|
|
|
|
|
if (sum < 0) {
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
}
|