순차적으로 접근해야 할 때 시작점과 끝점 2개의 점으로 접근할 데이터의 범위를 포함할 수 있다.const N = [3, 2, 4, 1, 2, 2, 1, 5];
const M = 5;
let answer = 0;
let sum = 0;
let end = 0;
// start를 고정시켜놓고 start를 차례대로 증가시킨다.
for (let start = 0; start < N.length; start++) {
// end를 가능한 만큼 이동시킨다.
while (sum < M && end < N.length) {
sum += N[end];
end += 1;
}
if (M === sum) answer += 1;
sum -= N[start];
}
console.log(answer);