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){intcount=0,currentSum=0;ArrayList<Integer>candidates=newArrayList<>();// Initial itemsfor(inti=0;i<m;i++){candidates.add(s.get(i));currentSum+=s.get(i);}//Check if satisfies the solutionif(currentSum==d)count++;// m - 1 as I already have the first 3 itemsfor(inti=m;i<s.size();i++){//Substract candidates[0] from sumcurrentSum-=candidates.remove(0);//Enqueue new itemcandidates.add(s.get(i));//Add new item to sumcurrentSum+=s.get(i);//Check if satisfies the solutionif(currentSum==d)count++;}returncount;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Subarray Division 1
You are viewing a single comment's thread. Return to all comments →
Java solution using a Queue: