计算子数组的数量,使得子数组中存在的元素的平均值大于子数组中不存在的元素的平均值
给定一个整数数组arr[] ,任务是计算子数组的数量,使得子数组中存在的元素的平均值大于子数组中不存在的元素的平均值。
例子:
Input: arr[] = {6, 3, 5}
Output: 3
The sub-arrays are {6}, {5} and {6, 3, 5} because their averages
are greater than {3, 5}, {6, 3} and {} respectively.
Input: arr[] = {2, 1, 4, 1}
Output: 5
方法:通过计算给定数组的前缀和数组可以很容易地解决这个问题。前缀 sum 数组的第 i 个元素将包含直到i的元素的总和。因此,可以使用前缀和数组找到任意两个索引i和j之间的元素之和。使用嵌套循环,找到所有可能的子数组,使其平均和大于数组中不存在的元素的平均值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count of sub-arrays
// such that the average of elements present
// in the sub-array is greater than the
// average of the elements not present
// in the sub-array
int countSubarrays(int a[], int n)
{
// Initialize the count variable
int count = 0;
// Initialize prefix sum array
int pre[n + 1] = { 0 };
// Preprocessing prefix sum
for (int i = 1; i < n + 1; i++) {
pre[i] = pre[i - 1] + a[i - 1];
}
for (int i = 1; i < n + 1; i++) {
for (int j = i; j < n + 1; j++) {
// Calculating sum and count
// to calculate averages
int sum1 = pre[j] - pre[i - 1], count1 = j - i + 1;
int sum2 = pre[n] - sum1, count2 = ((n - count1) == 0) ? 1 : (n - count1);
// Calculating averages
int includ = sum1 / count1;
int exclud = sum2 / count2;
// Increment count if including avg
// is greater than excluding avg
if (includ > exclud)
count++;
}
}
return count;
}
// Driver code
int main()
{
int arr[] = { 6, 3, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countSubarrays(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to return the count of sub-arrays
// such that the average of elements present
// in the sub-array is greater than the
// average of the elements not present
// in the sub-array
static int countSubarrays(int a[], int n)
{
// Initialize the count variable
int count = 0;
// Initialize prefix sum array
int []pre = new int[n + 1];
Arrays.fill(pre, 0);
// Preprocessing prefix sum
for (int i = 1; i < n + 1; i++)
{
pre[i] = pre[i - 1] + a[i - 1];
}
for (int i = 1; i < n + 1; i++)
{
for (int j = i; j < n + 1; j++)
{
// Calculating sum and count
// to calculate averages
int sum1 = pre[j] - pre[i - 1], count1 = j - i + 1;
int sum2 = pre[n] - sum1, count2 =
((n - count1) == 0) ? 1 : (n - count1);
// Calculating averages
int includ = sum1 / count1;
int exclud = sum2 / count2;
// Increment count if including avg
// is greater than excluding avg
if (includ > exclud)
count++;
}
}
return count;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 6, 3, 5 };
int n = arr.length;
System.out.println(countSubarrays(arr, n));
}
}
// This code is contributed by SURENDRA_GANGWAR
Python3
# Python3 implementation of the approach
# Function to return the count of sub-arrays
# such that the average of elements present
# in the sub-array is greater than the
# average of the elements not present
# in the sub-array
def countSubarrays(a, n):
# Initialize the count variable
count = 0
# Initialize prefix sum array
pre = [0 for i in range(n + 1)]
# Preprocessing prefix sum
for i in range(1, n + 1):
pre[i] = pre[i - 1] + a[i - 1]
for i in range(1, n + 1):
for j in range(i, n + 1):
# Calculating sum and count
# to calculate averages
sum1 = pre[j] - pre[i - 1]
count1 = j - i + 1
sum2 = pre[n] - sum1
if n-count1 == 0:
count2 = 1
else:
count2 = n - count1
# Calculating averages
includ = sum1 // count1
exclud = sum2 // count2
# Increment count if including avg
# is greater than excluding avg
if (includ > exclud):
count += 1
return count
# Driver code
arr = [6, 3, 5 ]
n = len(arr)
print(countSubarrays(arr, n))
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the count of sub-arrays
// such that the average of elements present
// in the sub-array is greater than the
// average of the elements not present
// in the sub-array
static int countSubarrays(int []a, int n)
{
// Initialize the count variable
int count = 0;
// Initialize prefix sum array
int []pre = new int[n + 1];
Array.Fill(pre, 0);
// Preprocessing prefix sum
for (int i = 1; i < n + 1; i++)
{
pre[i] = pre[i - 1] + a[i - 1];
}
for (int i = 1; i < n + 1; i++)
{
for (int j = i; j < n + 1; j++)
{
// Calculating sum and count
// to calculate averages
int sum1 = pre[j] - pre[i - 1], count1 = j - i + 1;
int sum2 = pre[n] - sum1, count2 =
((n - count1) == 0) ? 1 : (n - count1);
// Calculating averages
int includ = sum1 / count1;
int exclud = sum2 / count2;
// Increment count if including avg
// is greater than excluding avg
if (includ > exclud)
count++;
}
}
return count;
}
// Driver code
public static void Main()
{
int []arr = { 6, 3, 5 };
int n = arr.Length;
Console.WriteLine(countSubarrays(arr, n));
}
}
// This code is contributed by Akanksha Rai
PHP
$exclud)
$count++;
}
}
return $count;
}
// Driver code
$arr = array( 6, 3, 5 );
$n = count($arr) ;
echo countSubarrays($arr, $n);
// This code is contributed by Ryuga
?>
Javascript
输出:
3
时间复杂度: O(N^2) 其中 N 是数组的长度。