Find the subarray with least average
Given an array arr[] of size n and integer k such that k <= n.
Examples :
Become a success story instead of just reading about them. Prepare for coding interviews at Amazon and other top product-based companies with our Amazon Test Series. Includes topic-wise practice questions on all important DSA topics along with 10 practice contests of 2 hours each. Designed by industry experts that will surely help you practice and sharpen your programming skills. Wait no more, start your preparation today!
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 averagevoid 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 programint 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;} |
Java
// A Simple Java program to find// minimum average subarrayclass Test { static int arr[] = new int[] { 3, 7, 90, 20, 10, 50, 40 }; // Prints beginning and ending indexes of subarray // of size k with minimum average static void findMinAvgSubarray(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); } } System.out.println("Subarray between [" + res_index + ", " + (res_index + k - 1) + "] has minimum average"); } // Driver method to test the above function public static void main(String[] args) { int k = 3; // Subarray size findMinAvgSubarray(arr.length, k); }} |
Python3
# Python3 program to find# minimum average subarray# Prints beginning and ending# indexes of subarray of size k# with minimum averagedef findMinAvgSubarray(arr, n, k): # k must be smaller than or equal to n if (n < k): return 0 # Initialize beginning index of result res_index = 0 # Compute sum of first subarray of size k curr_sum = 0 for i in range(k): curr_sum += arr[i] # Initialize minimum sum as current sum min_sum = curr_sum # Traverse from (k + 1)'th # element to n'th element for i in range(k, n): # 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) print("Subarray between [", res_index, ", ", (res_index + k - 1), "] has minimum average")# Driver Codearr = [3, 7, 90, 20, 10, 50, 40]k = 3 # Subarray sizen = len(arr)findMinAvgSubarray(arr, n, k)# This code is contributed by Anant Agarwal. |
C#
// A Simple C# program to find// minimum average subarrayusing System;class Test { static int[] arr = new int[] { 3, 7, 90, 20, 10, 50, 40 }; // Prints beginning and ending indexes of subarray // of size k with minimum average static void findMinAvgSubarray(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); } } Console.Write("Subarray between [" + res_index + ", " + (res_index + k - 1) + "] has minimum average"); } // Driver method to test the above function public static void Main() { int k = 3; // Subarray size findMinAvgSubarray(arr.Length, k); }}// This code is contributed by nitin mittal. |
PHP
<?php// A Simple PHP program to find// minimum average subarray// Prints beginning and ending// indexes of subarray of size// k with minimum averagefunction findMinAvgSubarray($arr, $n, $k){ // k must be smaller // than or equal to n if ($n < $k) return; // Initialize beginning // index of result $res_index = 0; // Compute sum of first // subarray of size k $curr_sum = 0; for ($i = 0; $i < $k; $i++) $curr_sum += $arr[$i]; // Initialize minimum sum // as current sum $min_sum = $curr_sum; // Traverse from (k+1)'th element // to n'th element for ( $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); } } echo "Subarray between [" ,$res_index , ", " ,$res_index + $k - 1, "] has minimum average";} // Driver Code $arr = array(3, 7, 90, 20, 10, 50, 40); // Subarray size $k = 3; $n = sizeof ($arr) / sizeof ($arr[0]); findMinAvgSubarray($arr, $n, $k); return 0;// This code is contributed by nitin mittal.?> |
Javascript
<script>// A Simple JavaScript program to find// minimum average subarray// Prints beginning and ending indexes// of subarray of size k with minimum averagefunction findMinAvgSubarray(arr, n, k){ // k must be smaller than or equal to n if (n < k) return; // Initialize beginning index of result let res_index = 0; // Compute sum of first subarray of size k let curr_sum = 0; for(let i = 0; i < k; i++) curr_sum += arr[i]; // Initialize minimum sum as current sum let min_sum = curr_sum; // Traverse from (k+1)'th element // to n'th element for(let 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); } } document.write("Subarray between [" + res_index + ", " + (res_index + k - 1) + "] has minimum average");}// Driver codelet arr = [ 3, 7, 90, 20, 10, 50, 40 ];// Subarray sizelet k = 3;let n = arr.length;findMinAvgSubarray(arr, n, k);// This code is contributed by Surbhi Tyagi.</script> |
Output:
Subarray between [3, 5] has minimum average
Time Complexity: O(n)
Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



