Skip to content

Commit 16e4235

Browse files
Create Sum of Odd length Subarrays
1 parent 99d4df8 commit 16e4235

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
9+
let oddsum = 0;
10+
11+
for (let i = 0; i < arr.length; i++) {
12+
let prevsum = 0;
13+
for (let j = i; j < arr.length; j++) {
14+
prevsum = prevsum + arr[j];
15+
16+
let length = j-i + 1;
17+
18+
if (length % 2 === 1) {
19+
oddsum = oddsum + prevsum;
20+
}
21+
}
22+
}
23+
return oddsum;
24+
25+
26+
}

0 commit comments

Comments
 (0)