Javascript Program to Find the subarray with least average
Last Updated :
17 Sep, 2024
Improve
Given an array arr[] of size n and integer k such that k <= n.
Examples :
Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3
Output: Subarray between indexes 3 and 5
The subarray {20, 10, 50} has the least average
among all subarrays of size 3.
Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2
Output: Subarray between [4, 5] has minimum average
We strongly recommend that you click here and practice it, before moving on to the solution.
A Simple Solution is to consider every element as beginning of subarray of size k and compute sum of subarray starting with this element. Time complexity of this solution is O(nk).
An Efficient Solution is to solve the above problem in O(n) time and O(1) extra space. The idea is to use sliding window of size k. Keep track of sum of current k elements. To compute sum of current window, remove first element of previous window and add current element (last element of current window).
1) Initialize res_index = 0 // Beginning of result index
2) Find sum of first k elements. Let this sum be 'curr_sum'
3) Initialize min_sum = sum
4) Iterate from (k+1)'th to n'th element, do following
for every element arr[i]
a) curr_sum = curr_sum + arr[i] - arr[i-k]
b) If curr_sum < min_sum
res_index = (i-k+1)
5) Print res_index and res_index+k-1 as beginning and ending
indexes of resultant subarray.
Below is the implementation of above algorithm.
// A Simple JavaScript program to find
// minimum average subarray
// Prints beginning and ending indexes
// of subarray of size k with minimum average
function findMinAvgSubarray(arr, n, k) {
// k must be smaller than or equal to n
if (n < k)
return;
// Initialize beginning index of result
let res_index = 0;
// Compute sum of first subarray of size k
let curr_sum = 0;
for (let i = 0; i < k; i++)
curr_sum += arr[i];
// Initialize minimum sum as current sum
let min_sum = curr_sum;
// Traverse from (k+1)'th element
// to n'th element
for (let i = k; i < n; i++) {
// Add current item and remove first
// item of previous subarray
curr_sum += arr[i] - arr[i - k];
// Update result if needed
if (curr_sum < min_sum) {
min_sum = curr_sum;
res_index = (i - k + 1);
}
}
console.log("Subarray between [" + res_index +
", " + (res_index + k - 1) + "] has minimum average");
}
// Driver code
let arr = [3, 7, 90, 20, 10, 50, 40];
// Subarray size
let k = 3;
let n = arr.length;
findMinAvgSubarray(arr, n, k);
48
1
// A Simple JavaScript program to find
2
// minimum average subarray
3
4
// Prints beginning and ending indexes
5
// of subarray of size k with minimum average
6
function findMinAvgSubarray(arr, n, k) {
7
8
// k must be smaller than or equal to n
9
if (n < k)
10
return;
11
12
// Initialize beginning index of result
13
let res_index = 0;
14
15
// Compute sum of first subarray of size k
16
let curr_sum = 0;
17
for (let i = 0; i < k; i++)
18
curr_sum += arr[i];
19
20
// Initialize minimum sum as current sum
21
let min_sum = curr_sum;
22
23
// Traverse from (k+1)'th element
24
// to n'th element
25
for (let i = k; i < n; i++) {
26
27
// Add current item and remove first
28
// item of previous subarray
29
curr_sum += arr[i] - arr[i - k];
30
31
// Update result if needed
32
if (curr_sum < min_sum) {
33
min_sum = curr_sum;
34
res_index = (i - k + 1);
35
}
36
}
37
console.log("Subarray between [" + res_index +
38
", " + (res_index + k - 1) + "] has minimum average");
39
}
40
41
// Driver code
42
let arr = [3, 7, 90, 20, 10, 50, 40];
43
44
// Subarray size
45
let k = 3;
46
let n = arr.length;
47
48
findMinAvgSubarray(arr, n, k);
Output:
Subarray between [3, 5] has minimum average
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Please refer complete article on Find the subarray with least average for more details!