-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankJava0029.java
More file actions
42 lines (32 loc) · 920 Bytes
/
HackerrankJava0029.java
File metadata and controls
42 lines (32 loc) · 920 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
41
42
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class HackerrankJava0029 {
public static void main(String[] args) {
// Java Subarray
// https://www.hackerrank.com/challenges/java-negative-subarray/problem?isFullScreen=true
Scanner scanner = new Scanner(System.in);
int numberOfValue = scanner.nextInt();
List<Integer> arr = new ArrayList<>();
for (int i = 0; i < numberOfValue; i++) {
arr.add(scanner.nextInt());
}
printResult(arr);
scanner.close();
}
public static void printResult(List<Integer> arr) {
int numberOfNegative = 0;
for (int i = 0; i < arr.size(); i++) {
for (int j = 1; j <= arr.size(); j++) {
int toIndex = i + j;
if (toIndex > arr.size()) {
break;
}
if (arr.subList(i, toIndex).stream().reduce(0, (a, b) -> a + b) < 0) {
numberOfNegative++;
}
}
}
System.out.println(numberOfNegative);
}
}