-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava Subarray
More file actions
40 lines (32 loc) · 760 Bytes
/
Copy pathJava Subarray
File metadata and controls
40 lines (32 loc) · 760 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
34
35
36
37
38
39
40
/* Output Format
Print the number of subarrays of having negative sums.
Sample Input
5
1 -2 4 -5 1
Sample Output
9 */
code
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] array1= new int[n];
for(int i=0;i<n;i++){
array1[i]=sc.nextInt();
}
int count=0;
for(int j=0;j<n;j++){
int sum=0;
for(int k=j;k<n;k++){
sum=array1[k]+sum;
if(sum<0){
count++;
}
}
}
System.out.println(count);
}
}