You are given a sequence of N numbers.
Write a program to find how many times in this sequence the sum of consecutive subsequences is less than or equal to a specific number M.
If N=5, M=5 and the sequence is
1 3 1 2 3
A continuous subsequence whose sum is 5 or less is {1}, {3}, {1}, {2}, {3}, {1, 3}, {3, 1}, {1, 2}, {2 , 3}, {1, 3, 1}, for a total of 10.
55
1 3 1 2 3
Print the number of cases on the first line.
10
function solution(m, arr) {
let answer = 0,
start = 0,
index = 0,
sum = 0;
while (index < arr.length) {
sum += arr[index++];
if (sum <= m) {
answer++;
} else {
start++;
index = start;
sum = 0;
}
}
if (arr[arr.length - 1] <= m) answer++;
return answer;
}
function solution(m, arr) {
let answer = 0,
sum = 0,
lt = 0;
for (let rt = 0; rt < arr.length; rt++) {
sum += arr[rt];
while (sum > m) {
sum -= arr[lt++];
}
answer += rt - lt + 1;
}
return answer;
}