Created
April 6, 2023 02:51
-
-
Save LuisitoRizado/312e208326a939c9d690c582a640b1ae to your computer and use it in GitHub Desktop.
This a solution for the 'Subarray Division' challenge by Hackerrank, you can use it free
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function birthday(s, d, m) { | |
// Write your code here | |
//we have to travel the array searching for the requirements | |
//EXAMPLE: | |
//s= [2,2,2,1,3,1] | |
//m = 2 | |
//p = 4 | |
//First: we travel the array searching i to i+m | |
let founds = 0; | |
let splicedArray; | |
let sum; | |
for(let i = 0; i<s.length; i++){ | |
//splice the array | |
splicedArray = s.slice(i,(i+m)); | |
//If the sum of all the values in the splicedArray is equals to p, then | |
//add to counter | |
sum = splicedArray.reduce((element1,element2)=>{ | |
return element1+element2; | |
},0) | |
//Compare if are equal | |
if(sum==d){ | |
founds++ | |
} | |
} | |
return founds; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment