给定一个数组arr []和两个整数X和Y。任务是找到一个大小至少为X和最大Y为平均值(最大的子数组)的子数组。
例子:
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
方法:对从X到Y大小的每个大小的子数组进行迭代,并找到所有此类子数组中的最大平均值。我们可以使用两个嵌套的for循环来迭代大小从X到Y的所有子数组。为了减少时间复杂度,我们可以使用前缀求和数组来获取O(1)复杂度的任何子数组的和。
下面是上述方法的实现:
C++
// 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
// 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
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
?>
Javascript
输出:
7.5
时间复杂度: O(N *(YX))
辅助空间: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。