# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ implementation of the approach #include 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 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 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# 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

 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 ?>

Output:
7.5

Time Complexity: O(N * (Y-X))
Auxiliary Space: O(N)

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :