-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
33 lines (28 loc) · 841 Bytes
/
Solution.java
File metadata and controls
33 lines (28 loc) · 841 Bytes
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;
public class Solution {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// Scan for array size
int n = sc.nextInt();
sc.nextLine();
// Populate the array
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
sc.close();
System.out.println(findNegativeSubs(arr));
}
private static int findNegativeSubs(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
int sum = arr[i];
if (sum < 0) count++;
for (int j = i + 1; j < arr.length; j++) {
sum += arr[j];
if (sum < 0) count++;
}
}
return count;
}
}