-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution001_1.java
42 lines (32 loc) · 914 Bytes
/
Solution001_1.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
35
36
37
38
39
40
41
42
import java.io.*;
import java.util.*;
public class Solution001_1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] numbers = new int[n];
for (int i = 0; i < n; i++) {
numbers[i] = s.nextInt();
}
System.out.println(solution(numbers));
}
public static int solution(int[] arr) {
int counter = 0;
int n = arr.length;
for (int i = 0; i < n; i++) {
int totalSum = arr[i];
if (totalSum < 0) {
counter++;
}
for (int j = i + 1; j < n; j++) {
totalSum += arr[j];
if (totalSum < 0) {
counter++;
}
}
}
return counter;
}
}
// https://www.hackerrank.com/challenges/java-negative-subarray
// Time O(n^2)