hacker rank 28th problem(java subarray)

28. Java Subarray

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

public class Solution {
   public static void main(String[] args) {
    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);
}
}

Compiler Message
Success
Input (stdin)Download
5
1 -2 4 -5 1
{-truncated-}
Download to view the full testcase
Expected OutputDownload
9

Comments

Popular posts from this blog

Amazon Web Services

Google Code-In mentorship experience :)

Hacker Rank all java and python problem solutions