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.
A well optimized Javascript solution uses a sliding window technique.
O(n) in time complexity where n is the length of the array.
functionbirthday(s,d,m){letstart=0;letend=m;letsubarray=[];letnoOfSegments=0;// If the array is empty or no of squares < m first criteria becomes false// return 0;if(!s.length||s.length<m){return0;}// If no of squares == `m` check whether the sum of the squares == d// If so return 1 else 0if(s.length===m){if(s.reduce((sum,currentValue)=>sum+currentValue)===d){return1;}return0;}// Checking each and every subarray combinations using sliding window technique// If mentioned criteria meets add 1 to the noOfSegmentwhile(end<=s.length){subarray=[];for(leti=start;i<end;i++){subarray.push(s[i]);}if(subarray.reduce((sum,currentValue)=>sum+currentValue)===d){noOfSegments++;}start++;end++}returnnoOfSegments;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Subarray Division
You are viewing a single comment's thread. Return to all comments →
A well optimized Javascript solution uses a sliding window technique. O(n) in time complexity where n is the length of the array.