Java Subarray Hackerrank Solution
For Explanation Check Video:
Sample Input
5
1 -2 4 -5 1
Sample Output
9
Code:
import java.io.*;import java.util.*;
public class Solution {
public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i] = scn.nextInt(); } int count = 0; for(int i=0;i<n;i++){ int sum = 0; for(int j =i;j<n;j++){ sum = sum + arr[j]; if(sum<0){ count++; } } } System.out.println(count); }}
Comments
Post a Comment