Maximum average of a subarray of size of atleast X and atmost Y
Given an array arr[] and two integers X and Y. The task is to find a sub-array of size of atleast X and atmost Y with the maximum average (average of the elements of the sub-array).
Examples:
Input: arr[] = {1, 2, 3, 4, 5} X = 2, Y = 3
Output: 4.5
We can take the sub-array {4, 5} which gives us the maximum average.
Input: arr[] = {6, 7, 8, 3, 2, 4, 2} X = 2, Y = 4
Output: 7.5
Approach: Iterate over every sub-array of size starting from X to size Y and find the maximum average among all such sub-arrays. We can use two nested for loops to iterate over all sub-arrays whose size varies from X to Y. To reduce the time complexity, we can use prefix sum array to get the sum of any sub-array in O(1) complexity.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum average // of the sub-array with size // atleast x and atmost y double maxAvgSubArray(int a[], int n, int x, int y) { // Calculate the prefix sum array int prefix[n]; prefix[0] = a[0]; for (int i = 1; i < n; i++) prefix[i] = prefix[i - 1] + a[i]; double maximum = 0; // Iterate over all sub-arrays for (int i = 0; i < n; i++) { // Sub-arrays of size X to Y for (int j = i + x - 1; j < i + y && j < n; j++) { // Get the sum of the sub-array double sum = prefix[j]; if (i > 0) sum -= prefix[i - 1]; // Find average of sub-array double current = sum / (double)(j - i + 1); // Store the maximum of average maximum = max(maximum, current); } } return maximum; } // Driver code int main() { int a[] = { 6, 7, 8, 3, 2, 4, 2 }; int X = 2, Y = 4; int n = sizeof(a) / sizeof(a[0]); cout << maxAvgSubArray(a, n, X, Y); return 0; } |
Java
// Java implementation of the approach class GfG { // Function to return the maximum average // of the sub-array with size // atleast x and atmost y static double maxAvgSubArray(int a[], int n, int x, int y) { // Calculate the prefix sum array int prefix[] = new int[n]; prefix[0] = a[0]; for (int i = 1; i < n; i++) prefix[i] = prefix[i - 1] + a[i]; double maximum = 0; // Iterate over all sub-arrays for (int i = 0; i < n; i++) { // Sub-arrays of size X to Y for (int j = i + x - 1; j < i + y && j < n; j++) { // Get the sum of the sub-array double sum = prefix[j]; if (i > 0) sum -= prefix[i - 1]; // Find average of sub-array double current = sum / (double)(j - i + 1); // Store the maximum of average maximum = Math.max(maximum, current); } } return maximum; } // Driver code public static void main(String[] args) { int a[] = { 6, 7, 8, 3, 2, 4, 2 }; int X = 2, Y = 4; int n = a.length; System.out.println(maxAvgSubArray(a, n, X, Y)); } } |
Python3
# Python3 implementation of the approach # Function to return the maximum average # of the sub-array with size # atleast x and atmost y def maxAvgSubArray(a, n, x, y) : # Calculate the prefix sum array prefix = [0] * n ; prefix[0] = a[0]; for i in range(1, n) : prefix[i] = prefix[i - 1] + a[i]; maximum = 0; # Iterate over all sub-arrays for i in range(n) : j = i + x - 1 # Sub-arrays of size X to Y while(j < i + y and j < n) : # Get the sum of the sub-array sum = prefix[j]; if (i > 0) : sum -= prefix[i - 1]; # Find average of sub-array current = sum / (j - i + 1); # Store the maximum of average maximum = max(maximum, current); j += 1 return maximum; # Driver code if __name__ == "__main__" : a = [ 6, 7, 8, 3, 2, 4, 2 ]; X = 2; Y = 4; n = len(a); print(maxAvgSubArray(a, n, X, Y)); # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum // average of the sub-array with // size atleast x and atmost y public static double maxAvgSubArray(int[] a, int n, int x, int y) { // Calculate the prefix sum array int[] prefix = new int[n]; prefix[0] = a[0]; for (int i = 1; i < n; i++) { prefix[i] = prefix[i - 1] + a[i]; } double maximum = 0; // Iterate over all sub-arrays for (int i = 0; i < n; i++) { // Sub-arrays of size X to Y for (int j = i + x - 1; j < i + y && j < n; j++) { // Get the sum of the sub-array double sum = prefix[j]; if (i > 0) { sum -= prefix[i - 1]; } // Find average of sub-array double current = sum / (double)(j - i + 1); // Store the maximum of average maximum = Math.Max(maximum, current); } } return maximum; } // Driver code public static void Main(string[] args) { int[] a = new int[] {6, 7, 8, 3, 2, 4, 2}; int X = 2, Y = 4; int n = a.Length; Console.WriteLine(maxAvgSubArray(a, n, X, Y)); } } // This code is contributed by Shrikant13 |
PHP
<?php // PHP implementation of the approach // Function to return the maximum average // of the sub-array with size // atleast x and atmost y function maxAvgSubArray($a, $n, $x, $y) { // Calculate the prefix sum array $prefix = array(); $prefix[0] = $a[0]; for ($i = 1; $i < $n; $i++) $prefix[$i] = $prefix[$i - 1] + $a[$i]; $maximum = 0; // Iterate over all sub-arrays for ($i = 0; $i < $n; $i++) { // Sub-arrays of size X to Y for ($j = $i + $x - 1; $j < $i + $y && $j < $n; $j++) { // Get the sum of the sub-array $sum = $prefix[$j]; if ($i > 0) $sum -= $prefix[$i - 1]; // Find average of sub-array $current = ($sum / ($j - $i + 1)); // Store the maximum of average $maximum = max($maximum, $current); } } return $maximum; } // Driver code $a = array(6, 7, 8, 3, 2, 4, 2); $X = 2; $Y = 4; $n = sizeof($a); echo maxAvgSubArray($a, $n, $X, $Y); // This code is contributed by Akanksha Rai ?> |
7.5
Time Complexity: O(N * (Y-X))
Auxiliary Space: O(N)
Recommended Posts:
- Maximize the maximum subarray sum after removing atmost one element
- Find maximum average subarray of k length
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Size of The Subarray With Maximum Sum
- Maximum circular subarray sum of size K
- Maximum Unique Element in every subarray of size K
- Find maximum (or minimum) sum of a subarray of size k
- Longest Subarray having sum of elements atmost 'k'
- Find the subarray with least average
- Maximum sum of non-overlapping subarrays of length atmost K
- Maximum number of fixed points using atmost 1 swap
- Longest subarray having average greater than or equal to x | Set-2
- Longest subarray having average greater than or equal to x
- Subarray of size k with given sum
- Maximum length of subarray such that sum of the subarray is even
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

