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){
HR0825 hr = new HR0825();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int[] arr = new int[a];
for(int i =0; i<a; i++){
arr[i]=sc.nextInt();
}
int totalSum =0;
for(int i=0; i<a; i++){
if(i+1<a){
for(int j=i+1;j<a;j++){
if(hr.sumSubArray(arr, i, j) <0 ) totalSum +=1;
}
}
if(arr[i]<0) totalSum+=1;
}
System.out.println(totalSum);
sc.close();
}
//Hà m tÃnh tổng cá»§a SubArray
public int sumSubArray(int[]arr, int i, int j){
int[] subArr = Arrays.copyOfRange(arr, i, j+1);
int sum = Arrays.stream(subArr).sum();
return sum;
}
}
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 →
import java.util.*; public class HR0825 {
}