Python3 Program to Find the subarray with least average
Last Updated :
06 Sep, 2024
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.
Python3
# Python3 program to find
# minimum average subarray
# Prints beginning and ending
# indexes of subarray of size k
# with minimum average
def 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 Code
arr = [3, 7, 90, 20, 10, 50, 40]
k = 3 # Subarray size
n = len(arr)
findMinAvgSubarray(arr, n, k)
# This code is contributed by Anant Agarwal.
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 average of a list in python We are given a list of numbers and the task is to find the average (or mean) value of its elements. For example, if we have a list nums = [10, 20, 30, 40], the sum of the elements is 10 + 20 + 30 + 40, which equals 100. The number of elements in the list is 4. So, the average is calculated as 100 /
2 min read
Python Program for Median of two sorted arrays of same size Write a Python program for a given 2 sorted arrays A and B of size n each. the task is to find the median of the array obtained by merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n)). Examples: Input: ar1[] = {1, 12, 15, 26, 38} ar2[] = {2, 13, 17, 30, 45}Output:
6 min read
Find the average of an unknown number of inputs in Python Prerequisites: *args and **kwargs in Python The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyword, variable-length argument list. The syntax is to use the symbol * to take in a variable number of argu
3 min read
Create an array which is the average of every consecutive subarray of given size using NumPy In this article, we will see the program for creating an array of elements in which every element is the average of every consecutive subarrays of size k of a given numpy array of size n such that k is a factor of n i.e. (n%k==0). This task can be done by using numpy.mean() and numpy.reshape() funct
2 min read