
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- Subarray sum with at least two elements in JavaScript
- JavaScript Program to Find if there is a subarray with 0 sum
- JavaScript program for Size of the Subarray with Maximum Sum
- Program to find largest average of sublist whose size at least k in Python
- Shortest Subarray with Sum at Least K in C++
- Maximum average of a specific length of subarray in JavaScript
- Largest sum subarray with at-least k numbers in C++
- Find maximum average subarray of k length in C++
- Maximum average of a subarray of size of at least X and at most Y in C++
- Subarray with the greatest product in JavaScript
- JavaScript Program for Maximum Product Subarray
- Program to find maximum length of subarray with positive product in Python
- Golang program to find maximum sum of a subarray with length k
- Maximum Average Subarray I in C++
- Maximum Average Subarray II in C++
