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[]nums=newint[n];int[]prefixSum=newint[n];//create prefix sum of streaming numbersprefixSum[0]=sc.nextInt();for(inti=1;i<n;i++){inttemp=sc.nextInt();prefixSum[i]=prefixSum[i-1]+temp;}intcount=0;for(intend=n-1;end>=0;end--){for(intstart=0;start<=end;start++){intsum=0;if(start==0)sum=prefixSum[end];elsesum=prefixSum[end]-prefixSum[start-1];if(sum<0)count++;}}System.out.println(count);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Java Subarray
You are viewing a single comment's thread. Return to all comments →
Using Prefix sum