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 static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int sum=0;
int negSub = 0;
String[] arr = new String[num];
scan.nextLine();
String n = scan.nextLine();
arr = n.split(" ");
for(int i=0;i<num;i++){
for(int j=i+1;j<=num;j++){
sum=0;
String[] subArray = Arrays.copyOfRange(arr, i,j);
//System.out.println(Arrays.toString(subArray));
for(int w=0;w<subArray.length;w++){
sum+=Integer.parseInt(subArray[w]);
}
if(sum<0){
negSub++;
}
//System.out.println(sum);
}
}
System.out.println(negSub);
scan.close();
}
}
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 →
My solution - As simple as it can get:
import java.io.; import java.util.;
public class Solution {
} }