forked from SedaKunda/hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1DArray.java
More file actions
36 lines (29 loc) · 1.02 KB
/
1DArray.java
File metadata and controls
36 lines (29 loc) · 1.02 KB
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
/*
This problem will test your knowledge on java array.
You are given an array of nn integers. A sub-array is "Negative" if sum of all the integers in that sub-array is negative. Count the number of "Negative sub-arrays" in the input array.
Note: Subarrays are contiguous chunks of the main array. For example if the array is {1,2,3,5} then some of the subarrays are {1}, {1,2,3}, {2,3,5}, {1,2,3,5} etc. But {1,2,5} is not an subarray as it is not contiguous.
*/
import java.math.BigDecimal;
import java.util.*;
class 1DArray{
public static void main(String []argh)
{
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int []s=new int[n];
int count = 0;
for(int i=0;i<n;i++) {
s[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
sum+=s[j];
if (sum<0) {
count++;
}
}
}
System.out.println (count);
}
}