C++ Program to Find the subarray with least average Last Updated : 29 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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 averageWe 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. 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! Comment More infoAdvertise with us Next Article C++ Program to Find the subarray with least average kartik Follow Improve Article Tags : Mathematical C++ Programs C++ DSA Arrays Amazon +2 More Practice Tags : AmazonCPPArraysMathematical Similar Reads C++ Program For Finding Subarray With Given Sum - Set 1 (Nonnegative Numbers) Given an unsorted array of non-negative integers, find a continuous subarray that adds to a given number. Examples : Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Output: Sum found between indexes 2 and 4 Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33 Input: arr[] = {1, 4, 0, 0, 3, 10, 5 min read C++ Program for Mean of range in array Given an array of n integers. You are given q queries. Write a program to print the floor value of mean in range l to r for each query in a new line. Examples : Input : arr[] = {1, 2, 3, 4, 5} q = 3 0 2 1 3 0 4 Output : 2 3 3 Here for 0 to 2 (1 + 2 + 3) / 3 = 2 Input : arr[] = {6, 7, 8, 10} q = 2 0 4 min read How to Find the Median of Array in C++? In C++, the array is a collection of elements of the same type, In this article, we will learn how to find the median of the array in C++. The median of the array will be the middle element if the number of elements is odd or the average of two middle elements if the number of elements is even in th 2 min read C++ Program For Average of an Array (Iterative and Recursive) Given an array, the task is to find the average of that array. Average is the sum of the array elements divided by the number of elements. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Sum of the elements is 1+2+3+4+5 = 15 and total number of elements is 5.So average is 15/5 = 3 Input: arr[] = {5 3 min read C++ Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9}; 4 min read How to Find the Average of All Elements in a Vector in C++? The average of all elements in vector is defined as the ratio of sum of all elements in vector to the number of elements in vector. In this article, we will learn how to find the average of all elements in vector in C++.The simplest method to find the average of all elements of vector is by using ac 2 min read How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma 2 min read C++ Program to Find the Minimum and Maximum Element of an Array Given an array, write functions to find the minimum and maximum elements in it. Example: C++ // C++ program to find minimum (or maximum) element // in an array. #include <bits/stdc++.h> using namespace std; int getMin(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = 3 min read Queries to find the minimum index in given array having at least value X Given an array arr[] of size N and an array Q[] consisting of M integers, each representing a query, the task for each query Q[i] is to find the smallest index of an array element whose value is greater than or equal to Q[i]. If no such index exists, then print -1. Examples: Input: arr[] = { 1, 9 }, 9 min read How to Find Average of All Elements in a List in C++? In C++, the STL provides a list container that is a doubly-linked list that allows storing the data in non-continuous memory locations. In this article, we will learn how to find the average of all elements in a list in C++. Example: Input:myList = {10, 20, 30, 40, 50};Output: Average of all element 2 min read Like