-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsoln.java
34 lines (29 loc) · 913 Bytes
/
soln.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner;
// A subarray must be contiguous. There are O(n^2) contiguous subarrays.
// Time Complexity: O(n^2)
// Space Complexity: O(1)
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = scan.nextInt();
}
scan.close();
System.out.println(negativeSubarrays(array));
}
private static int negativeSubarrays(int[] array) {
int count = 0;
for (int i = 0; i < array.length; i++) {
int sum = 0;
for (int j = i; j < array.length; j++) {
sum += array[j];
if (sum < 0) {
count++;
}
}
}
return count;
}
}