We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class Solution {
public static int sumArr(int[] a)
{
int sum = 0;
for (int num : a){
sum = sum + num;
}
return sum;
}
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 scn = new Scanner(System.in);
int n = scn.nextInt();
int[] a = new int [n];
int result = 0;
for (int i = 0; i<n; i++)
{
a[i] = scn.nextInt();
}
for (int j =0; j<n; j++)
{
for (int k =j; k<n; k++)
{
int summ = 0;
for (int l=j; l<=k; l++)
{
summ = summ + a[l];
if (summ < 0)
{
result = result +1;
}
}
}
}
System.out.println(result);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Subarray
You are viewing a single comment's thread. Return to all comments →
whats wrong in this
import java.io.; import java.util.; import java.util.Arrays;
public class Solution { public static int sumArr(int[] a) { int sum = 0; for (int num : a){ sum = sum + num; } return sum; }
}