Given an array of N integers, find and print its number of negative subarrays (i.e sub arrays having negative summation) on a new line.
My code is taking \$\mathcal{O}(n^2)\$ time. How can I improve this?
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numberOfInts = sc.nextInt();
int[] arr = new int[numberOfInts];
for (int i = 0; i < numberOfInts; i++) {
arr[i] = sc.nextInt();
}
int count = 0;
for (int i = 0; i < numberOfInts - 2; i++) {
int sum = arr[i];
if (arr[i] < 0) {
count++;
}
for (int j = i + 1; j < numberOfInts; j++) {
sum = sum + arr[j];
if (sum < 0) {
count++;
}
}
}
if (arr[numberOfInts - 1] < 0) {
count++;
}
System.out.println(count);
}
}