Skip to content

Instantly share code, notes, and snippets.

@sundicide
Created October 13, 2022 14:14
Show Gist options
  • Select an option

  • Save sundicide/a16cee2914b3bf8ead06e6a0ce4e3bb6 to your computer and use it in GitHub Desktop.

Select an option

Save sundicide/a16cee2914b3bf8ead06e6a0ce4e3bb6 to your computer and use it in GitHub Desktop.
function solution(paramArr1, goal) {
let result = 0;
let lt = 0;
let sum = 0;
for (let rt = 0; rt < paramArr1.length; rt++) {
sum += paramArr1[rt];
if (sum === goal) result++;
while (sum >= goal) {
sum -= paramArr1[lt++];
if (sum === goal) result++;
}
}
return result;
}
const result = solution([1, 2, 1, 3, 1, 1, 1, 2], 6);
expectEqualTest(result, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment