-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subarray.java
34 lines (31 loc) · 883 Bytes
/
Subarray.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
34
import java.io.*;
import java.util.*;
public class Subarray
{
public static void main(String[] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
byte T = Byte.parseByte(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] arr = new int[T];
for(int i = 0; i < T; i++)
{
arr[i] = Integer.parseInt(st.nextToken());
}
int count = 0;
for (int i = 0; i < T; i++)
{
int sum = 0;
for (int j = i; j < T; j++)
{
sum += arr[j];
if (sum < 0)
{
// If the most recent sum is -ve, count++
count++;
}
}
}
System.out.println(count);
}
}