-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
36 lines (32 loc) · 1.04 KB
/
Solution.java
File metadata and controls
36 lines (32 loc) · 1.04 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
// https://www.hackerrank.com/challenges/java-negative-subarray/problem
import java.io.*;
import java.util.*;
public class Solution {
public static int getCountNegSums(int[] arr) {
int cnt = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
int k = i;
int sum = 0;
while (k <= j) {
sum += arr[k];
k++;
}
if (sum < 0) cnt++;
}
}
//return Integer.MIN_VALUE;
return cnt;
}
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 scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
while (n-- > 0) {
arr[arr.length - n - 1] = scanner.nextInt();
}
int result = getCountNegSums(arr);
System.out.println(result);
}
}