Skip to content

Instantly share code, notes, and snippets.

@icarito
Created February 18, 2025 16:58
Show Gist options
  • Save icarito/6bc000080ad90eea8e24a192d4c2f1b9 to your computer and use it in GitHub Desktop.
Save icarito/6bc000080ad90eea8e24a192d4c2f1b9 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
//1. Formar un arreglo principal
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int numbers [] = new int [n];
for ( int i = 0; i < n; i++){
numbers[i] = scanner.nextInt();
}
int sum;
int count = 0;
//2. Extraer subarreglos
for (int i=0; i <= n; i++)
{
// Pick ending point
for (int j=i; j<=n; j++)
{
sum = 0;
for (int num : Arrays.copyOfRange(numbers, i, j)) {
sum += num;
}
if (sum < 0) {
count++;
}
// System.out.println(sum);
}
}
//3. Devolver el numero posible de subsrrays con suma negativa
System.out.println(count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment