C++ Program to Find the subarray with least average
Last Updated :
29 Dec, 2021
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
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.
C++
// A Simple C++ program to find minimum average subarray
#include <bits/stdc++.h>
using namespace std;
// Prints beginning and ending indexes of subarray
// of size k with minimum average
void findMinAvgSubarray(int arr[], int n, int k)
{
// k must be smaller than or equal to n
if (n < k)
return;
// Initialize beginning index of result
int res_index = 0;
// Compute sum of first subarray of size k
int curr_sum = 0;
for (int i = 0; i < k; i++)
curr_sum += arr[i];
// Initialize minimum sum as current sum
int min_sum = curr_sum;
// Traverse from (k+1)'th element to n'th element
for (int 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);
}
}
cout << "Subarray between [" << res_index << ", "
<< res_index + k - 1 << "] has minimum average";
}
// Driver program
int main()
{
int arr[] = { 3, 7, 90, 20, 10, 50, 40 };
int k = 3; // Subarray size
int n = sizeof arr / sizeof arr[0];
findMinAvgSubarray(arr, n, k);
return 0;
}
Output:
Subarray between [3, 5] has minimum average
Time Complexity: O(n)
Auxiliary Space: O(1)
Please refer complete article on
Find the subarray with least average for more details!
Similar Reads
Javascript Program to Find the subarray with least average 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 = 3Output: Subarray between indexes 3 and 5The subarray {20, 10, 50} has the least average among all subarrays of size 3.Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2Output: Sub
3 min read
Find the subarray with least average 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
12 min read
Find maximum average subarray of k length Given an array with positive and negative numbers, find the maximum average subarray of the given length. Example: Input: arr[] = {1, 12, -5, -6, 50, 3}, k = 4Output: Maximum average subarray of length 4 begins at index 1.Maximum average is (12 - 5 - 6 + 50)/4 = 51/4Recommended PracticeMaximum avera
15+ min read
Smallest subarray with sum greater than a given value Given an array arr[] of integers and a number x, the task is to find the smallest subarray with a sum strictly greater than x.Examples:Input: x = 51, arr[] = [1, 4, 45, 6, 0, 19]Output: 3Explanation: Minimum length subarray is [4, 45, 6]Input: x = 100, arr[] = [1, 10, 5, 2, 7]Output: 0Explanation: N
15+ min read
Minimum in a Sorted and Rotated Array Given a sorted array of distinct elements arr[] of size n that is rotated at some unknown point, the task is to find the minimum element in it. Examples: Input: arr[] = [5, 6, 1, 2, 3, 4]Output: 1Explanation: 1 is the minimum element present in the array.Input: arr[] = [3, 1, 2]Output: 1Explanation:
9 min read
Mean of given array after removal of K percent of smallest and largest array elements Given an array arr[] and an integer K, the task is to remove K % percent array elements from the smallest and largest array elements and calculate the mean of the remaining array. Examples: Input: arr[] = {6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0}, K = 5Output: 4.00000Explanation:
6 min read