Skip to content

Commit 66507c6

Browse files
Create Sum of Odd length Subarrays
1 parent 08d8c6b commit 66507c6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
/**
3+
* @param {number[]} nums
4+
* @param {number} k
5+
* @return {void} Do not return anything, modify nums in-place instead.
6+
*/
7+
var sumOddLengthSubarrays = function (arr) {
8+
let oddsum = 0;
9+
10+
for (let i = 0; i < arr.length; i++) {
11+
let prevsum = 0;
12+
for (let j = i; j < arr.length; j++) {
13+
prevsum = prevsum + arr[j];
14+
15+
let length = j-i + 1;
16+
17+
if (length % 2 === 1) {
18+
oddsum = oddsum + prevsum;
19+
}
20+
}
21+
}
22+
return oddsum;
23+
}

0 commit comments

Comments
 (0)