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(findsmallestAverageSubarray(arr, k));

Explanation

  • The function findSmallestAverageSubarray takes an array of integers arr and an integer k as input, where k is the length of the subarray.

  • The function returns the subarray with the smallest average value.

  • The minAvg variable is initialized with the largest possible value of a floating-point number.

  • The minAvgStart variable is used to store the starting index of the subarray with the smallest average value.

  • The windowSum variable is used to store the sum of elements in the current subarray.

  • The outer loop for (let i = 0; i < arr.length - k + 1; i++) is used to iterate over all possible subarrays of length k in the given array arr.

  • The inner loop for (let j = 0; j < k; j++) is used to calculate the sum of elements in the current subarray.

  • The if (i === 0) block is used to calculate the sum of elements in the first subarray.

  • The else block is used to calculate the sum of elements in the remaining subarrays. It subtracts the first element of the previous subarray and adds the last element of the current subarray.

  • The windowAvg variable is used to store the average value of the current subarray.

  • The if (windowAvg < minAvg) block is used to update the minimum average value and the starting index of the subarray with the smallest average value.

  • Finally, the function returns the subarray with the smallest average value.

Updated on: 15-Mar-2023

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements