Python3 Program to Find the subarray with least average Last Updated : 23 Jul, 2025 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 = 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: 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 index2) Find sum of first k elements. Let this sum be 'curr_sum'3) Initialize min_sum = sum4) 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 averageTime 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 Python OOPs Concepts K kartik Follow Improve Article Tags : Python Amazon Practice Tags : Amazonpython Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 12 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. OOPs is a way of organizing code that uses objects and classes to represent real-world entities and their behavior. In OOPs, object has attributes thing th 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Like