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.
importjava.io.*;importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannersc=newScanner(System.in);intn=sc.nextInt();int[]arr=newint[n];for(inti=0;i<arr.length;i++){arr[i]=sc.nextInt();}intnumSubArrs=0;intsteps=1;while(true){for(inti=0;i<=arr.length-steps;i++){int[]subarr=Arrays.copyOfRange(arr,i,i+steps);intsum=Arrays.stream(subarr).reduce(0,Integer::sum);if(sum<0)numSubArrs++;}if(steps==arr.length)break;steps++;}System.out.println(numSubArrs);}}
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 →