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.
publicstaticintbirthday(List<Integer>s,intd,intm){// Write your code hereintcurrSize=0;intnumWays=0;if(s.size()<m)return0;// get sum of first segement (first m elements)for(inti=0;i<m;i++){currSize+=s.get(i);}// check sum == dif(currSize==d){numWays++;}// sliding window, subtract prev element and add next element to get sum of the next segment (size m)// starts at 1 because you already calculated first segement sum// ends on the last segment of size mfor(intj=1;j<s.size()-m+1;j++){currSize-=s.get(j-1);currSize+=s.get(j+m-1);if(currSize==d){numWays++;}}returnnumWays;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Subarray Division 2
You are viewing a single comment's thread. Return to all comments →
Java 8, Sliding Sum Technique, O(n)
I added some comments explaining the code