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.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{// Made a method to find the negative sum of sub arrayspublicstaticvoidnegativeSubArrays(intarr[]){// initialise current sumintcurrentSum=0;// initialise count to count the negative sumintcount=0;// Loop for Sub arraysfor(inti=0;i<arr.length;i++){intstart=i;for(intj=i;j<arr.length;j++){intend=j;currentSum=0;for(intk=start;k<=end;k++){currentSum+=arr[k];}// Check here, if the current sum of that particular array is // negative, then increase the count by 1if(currentSum<0){count++;}}}// Output the value to the screenSystem.out.println(count);}publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intn=sc.nextInt();intarr[]=newint[n];for(inti=0;i<arr.length;i++){arr[i]=sc.nextInt();}sc.close();negativeSubArrays(arr);}}
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 →