Header Ad

HackerRank java Subarray problem solution

In this HackerRank Java Subarray problem in the java programming language We define the following:

A subarray of an n-element array is an array composed from a contiguous block of the original array's elements. For example, if array = [1,2,3], then the subarrays are [1], [2], [3], [1,2], [2,3], and [1,2,3]. Something like [1,3] would not be a subarray as it's not a contiguous subsection of the original array.

The sum of an array is the total sum of its elements.

  • An array's sum is negative if the total sum of its elements is negative.
  • An array's sum is positive if the total sum of its elements is positive.

Given an array of  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;
    }
}

Post a Comment

0 Comments