In this HackerRank Java Subarray problem in the java programming language you have Given an array of n integers, find and print its number of negative subarrays on a new line.

HackerRank java Subarray problem solution


HackerRank java subarray problem solution.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    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 sc=new Scanner(System.in);
    int n=sc.nextInt();
    int[] array1= new int[n];
    for(int i=0;i<n;i++){
        array1[i]=sc.nextInt();
    }
    int count=0;
    for(int j=0;j<n;j++){
        int sum=0;
        for(int k=j;k<n;k++){
            sum=array1[k]+sum;
            if(sum<0){
                count++;
            }
        }
    }
    System.out.println(count);
    }
}


Second solution

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    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 sc = new Scanner(System.in); 
        int range = sc.nextInt();        
        int[] numbers = new int[range];
        HashMap<Integer, Integer> h = new HashMap<>();
        for (int i = 0; i < numbers.length; i++)
        {
            numbers[i] = sc.nextInt();
        }
        int counter = 0;
        for (int i = 0; i < numbers.length; i++){
            for (int j = i; j < numbers.length; j++){
                int sum = 0;
                for (int k = i; k <= j; k = k + 1){
                    sum = sum + numbers[k];
                }
                if (sum < 0){
                    counter = counter + 1;
                }
            }
            
        }
    
       System.out.println(counter);
}
}


The solution in Java 8 programming.

import java.io.*;
import java.util.*;

public class Solution {

    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 sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        int [] numbers = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int negCount = 0;
        for(int i = 0; i < n; i++){
            for(int j = i; j < n; j++){
                int subArr[] = null;
                /*if(i == j){
                    subArr = new int[1];
                    subArr[0] = numbers[i];
                }
                else *///if(i <= j)
                    subArr = Arrays.copyOfRange(numbers, i, j+1);
                //System.err.println(Arrays.toString(subArr));
                int sum = sumArray(subArr);
                //System.err.println("Sum = "+sum);
                if(sum < 0){
                    negCount++;
                    //System.err.println("Range = ["+i+":"+j+"]");
                }
            }
        }
        System.out.println(negCount);
        sc.close();
    }
    public static int sumArray(int [] arr){
        int sum = 0;
        if(arr != null){
            for(int i = 0; i < arr.length; i++){
                sum+=arr[i];
            }
        }
        return sum;
    }
}