大小为 K 且平均至少为 M 的子数组的计数
给定一个由N个整数和两个正整数K和M组成的数组arr[] ,任务是找到平均至少为 M的大小为K的子数组的数量。
例子:
Input: arr[] = {2, 3, 3, 4, 4, 4, 5, 6, 6}, K = 3, M = 4
Output: 4
Explanation:
Below are the subarrays of size K(= 3) whose average is at least M(= 4) as:
- arr[3, 5]: The average is 4 which is at least M(= 4).
- arr[4, 6]: The average is 4.33 which is at least M(= 4).
- arr[5, 7]: The average is 5 which is at least M(= 4).
- arr[6, 8]: The average is 5.66 which is at least M(= 4).
Therefore, the count of the subarray is given by 4.
Input: arr[] = {3, 6, 3, 2, 1, 3, 9] K = 2, M = 4
Output: 3
方法:给定的问题可以通过使用两个指针和滑动窗口技术来解决。请按照以下步骤解决给定的问题:
- 初始化一个变量,比如count为0 ,它存储所有可能的子数组的计数。
- 初始化一个变量,比如sum为0 ,它存储大小为K的子数组的元素之和。
- 找到前K个数组元素的总和并将其存储在变量sum中。如果sum的值至少为 M*K ,则将count的值增加1 。
- 使用变量i在[K, N – 1]范围内遍历给定数组arr[]并执行以下步骤:
- 将arr[i]的值添加到变量sum并从 sum 中减去arr[i – K]的值。
- 如果sum的值至少为 M*K ,则将count的值增加1 。
- 完成上述步骤后,将count的值打印为子数组的结果计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the subarrays of
// size K having average at least M
int countSubArrays(int arr[], int N,
int K, int M)
{
// Stores the resultant count of
// subarray
int count = 0;
// Stores the sum of subarrays of
// size K
int sum = 0;
// Add the values of first K elements
// to the sum
for (int i = 0; i < K; i++) {
sum += arr[i];
}
// Increment the count if the
// current subarray is valid
if (sum >= K * M)
count++;
// Traverse the given array
for (int i = K; i < N; i++) {
// Find the updated sum
sum += (arr[i] - arr[i - K]);
// Check if current subarray
// is valid or not
if (sum >= K * M)
count++;
}
// Return the count of subarrays
return count;
}
// Driver Code
int main()
{
int arr[] = { 3, 6, 3, 2, 1, 3, 9 };
int K = 2, M = 4;
int N = sizeof(arr) / sizeof(arr[0]);
cout << countSubArrays(arr, N, K, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Driver Code
public static void main(String[] args)
{
int[] arr = { 3, 6, 3, 2, 1, 3, 9 };
int K = 2, M = 4;
System.out.println(countSubArrays(arr, K, M));
}
// Function to count the subarrays of
// size K having average at least M
public static int countSubArrays(int[] arr, int K,
int M)
{
// Stores the resultant count of
// subarray
int count = 0;
// Stores the sum of subarrays of
// size K
int sum = 0;
// Add the values of first K elements
// to the sum
for (int i = 0; i < K; i++) {
sum += arr[i];
}
// Increment the count if the
// current subarray is valid
if (sum >= K * M)
count++;
// Traverse the given array
for (int i = K; i < arr.length; i++) {
// Find the updated sum
sum += (arr[i] - arr[i - K]);
// Check if current subarray
// is valid or not
if (sum >= K * M)
count++;
}
// Return the count of subarrays
return count;
}
}
// This code is contributed by Kdheeraj.
Python3
# Python 3 code for the above approach
# Function to count the subarrays of
# size K having average at least M
def countSubArrays(arr, N, K, M):
# Stores the resultant count of
# subarray
count = 0
# Stores the sum of subarrays of
# size K
sum = 0
# Add the values of first K elements
# to the sum
for i in range(K):
sum += arr[i]
# Increment the count if the
# current subarray is valid
if sum >= K*M:
count += 1
# Traverse the given array
for i in range(K, N):
# Find the updated sum
sum += (arr[i] - arr[i - K])
# Check if current subarray
# is valid or not
if sum >= K*M:
count += 1
# Return the count of subarrays
return count
# Driver Code
if __name__ == '__main__':
arr = [3, 6, 3, 2, 1, 3, 9]
K = 2
M = 4
N = len(arr)
count = countSubArrays(arr, N, K, M)
print(count)
# This code is contributed by Kdheeraj.
C#
// C# program for the above approach
using System;
public class GFG
{
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 3, 6, 3, 2, 1, 3, 9 };
int K = 2, M = 4;
Console.WriteLine(countSubArrays(arr, K, M));
}
// Function to count the subarrays of
// size K having average at least M
public static int countSubArrays(int[] arr, int K,
int M)
{
// Stores the resultant count of
// subarray
int count = 0;
// Stores the sum of subarrays of
// size K
int sum = 0;
// Add the values of first K elements
// to the sum
for (int i = 0; i < K; i++) {
sum += arr[i];
}
// Increment the count if the
// current subarray is valid
if (sum >= K * M)
count++;
// Traverse the given array
for (int i = K; i < arr.Length; i++) {
// Find the updated sum
sum += (arr[i] - arr[i - K]);
// Check if current subarray
// is valid or not
if (sum >= K * M)
count++;
}
// Return the count of subarrays
return count;
}
}
// This code is contributed by AnkThon
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)