Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 51b9706

Browse files
author
Swastikyadav
committed
Average of all contigious subarray
1 parent 4485127 commit 51b9706

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Find the average of all contiguous subarrays of size ‘5’ in the given array.
2+
3+
function averageOfSubarraySizek(arr, k) {
4+
let i = 0;
5+
let j = k - 1;
6+
let result = [];
7+
let subArraySum = arr.slice(i, j + 1).reduce((acc, cv) => acc + cv);
8+
9+
while (j + 1 <= arr.length) {
10+
subArrAverage = subArraySum / k;
11+
result.push(subArrAverage);
12+
13+
subArraySum -= arr[i];
14+
i++;
15+
j++;
16+
subArraySum += arr[j];
17+
}
18+
19+
return result;
20+
}
21+
22+
console.log(averageOfSubarraySizek([1, 3, 2, 6, -1, 4, 1, 8, 2], 5));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /