Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program to Find the subarray with least average
We are going to write a program that will find the subarray with the least average. To do this, we will iterate through the array and keep track of the current subarray and its sum. For each element, we will calculate the average of the current subarray and compare it with the minimum average we have seen so far. If it is lower, we will update the minimum average and the start and end indices of the subarray. At the end of the iteration, we will return the subarray with the least average.
Approach
To find the subarray with the least average, we can follow these steps:
Initialize two variables, start and end, to keep track of the starting and ending indices of the subarray.
Use a for loop to iterate through the array, keeping track of the current sum and the minimum average found so far.
At each iteration, compare the current sum with the minimum average and update the start and end variables if a new minimum is found.
If the current sum is greater than the minimum average, move the starting index forward until the sum is less than the minimum average.
Repeat steps 2-4 until the end of the array is reached.
The subarray with the least average is the one starting at start and ending at end.
Example
Given an array of integers, find the subarray with the smallest average value.
Here is a complete working example in JavaScript to solve the problem:
function findsmallestAverageSubarray(arr, k) {
let minAvg = Number.POSITIVE_INFINITY;
let minAvgStart = 0;
let windowSum = 0;
for (let i = 0; i < arr.length - k + 1; i++) {
if (i === 0) {
for (let j = 0; j < k; j++) {
windowSum += arr[j];
}
} else {
windowSum -= arr[i - 1];
windowSum += arr[i + k - 1];
}
let windowAvg = windowSum / k;
if (windowAvg < minAvg) {
minAvg = windowAvg;
minAvgStart = i;
}
}
return arr.slice(minAvgStart, minAvgStart + k);
}
const arr = [1, 3, 6, -3, -4, 2, 5];
const k = 4;
console.log("Array:", arr);
console.log("Subarray length (k):", k);
console.log("Subarray with least average:", findsmallestAverageSubarray(arr, k));
Array: [1, 3, 6, -3, -4, 2, 5] Subarray length (k): 4 Subarray with least average: [-3, -4, 2, 5]
How It Works
The algorithm uses a sliding window technique to efficiently calculate averages:
The function findSmallestAverageSubarray takes an array of integers arr and an integer k as input, where k is the length of the subarray.
The minAvg variable is initialized with the largest possible value of a floating-point number.
The minAvgStart variable stores the starting index of the subarray with the smallest average value.
The windowSum variable stores the sum of elements in the current subarray.
The outer loop iterates over all possible subarrays of length k in the given array.
For the first window (i === 0), we calculate the sum of all k elements.
For subsequent windows, we use the sliding window technique: subtract the element that goes out and add the element that comes in.
We calculate the average and update the minimum if a smaller average is found.
Time Complexity Analysis
The sliding window approach gives us O(n) time complexity, where n is the length of the array. This is much more efficient than calculating each window sum from scratch, which would be O(n*k).
Conclusion
This algorithm efficiently finds the subarray with the least average using a sliding window technique. The key insight is to reuse calculations from the previous window by sliding the window one position at a time.
