给定大小为N的数组arr []和整数K ,任务是查找大小为K的任何子数组中存在的偶数的最大数量。
例子:
Input: arr[] = { 2, 3, 5, 4, 7, 6 }, K = 3
Output: 2
Explanation:
Subarrays of size K(=3) with maximum count of even numbers are { arr[3], arr[4], arr[5] }
Therefore, the required output is 2
Input: arr[] = { 4, 3, 2, 6 }, K = 2
Output: 2
天真的方法:解决此问题的最简单方法是生成大小为K的所有可能的子数组,并对子数组中的偶数进行计数。最后,打印获得的最大计数。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the maximum count of
// even numbers from all the subarrays of
// size K
int maxEvenIntegers(int arr[], int N, int M)
{
// Stores the maximum count of even numbers
// from all the subarrays of size K
int ans = 0;
// Generate all subarrays of size K
for (int i = 0; i <= N - M; i++) {
// Store count of even numbers
// in current subarray of size K
int cnt = 0;
// Traverse the current subarray
for (int j = 0; j < M; j++) {
// If current element
// is an even number
if (arr[i + j] % 2 == 0)
cnt++;
}
// Update the answer
ans = max(ans, cnt);
}
// Return answer
return ans;
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 5, 4, 7, 6 };
int K = 3;
// Size of the input array
int N = sizeof(arr) / sizeof(arr[0]);
cout << maxEvenIntegers(arr, N, K) << endl;
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to find the maximum count of
// even numbers from all the subarrays of
// size K
static int maxEvenIntegers(int arr[], int N, int M)
{
// Stores the maximum count of even numbers
// from all the subarrays of size K
int ans = 0;
// Generate all subarrays of size K
for (int i = 0; i <= N - M; i++)
{
// Store count of even numbers
// in current subarray of size K
int cnt = 0;
// Traverse the current subarray
for (int j = 0; j < M; j++)
{
// If current element
// is an even number
if (arr[i + j] % 2 == 0)
cnt++;
}
// Update the answer
ans = Math.max(ans, cnt);
}
// Return answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 3, 5, 4, 7, 6 };
int K = 3;
// Size of the input array
int N = arr.length;
System.out.print(maxEvenIntegers(arr, N, K) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum count of
# even numbers from all the subarrays of
# size K
def maxEvenIntegers(arr, N, K):
# Stores the maximum count of even numbers
# from all the subarrays of size K
ans = 0
# Generate all subarrays of size K
for i in range(N-K+1):
# Store count of even numbers
# in current subarray of size K
cnt = 0
# Traverse the current subarray
for j in range(0, K):
if arr[i+j] % 2 == 0:
cnt += 1
# Update the answer
ans = max(cnt, ans)
# Return answer
return ans
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 5, 4, 7, 6]
K = 3
# Size of the input array
N = len(arr)
print(maxEvenIntegers(arr, N, K))
# This code is contributed by MuskanKalra1
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the maximum count of
// even numbers from all the subarrays of
// size K
static int maxEvenIntegers(int []arr, int N, int M)
{
// Stores the maximum count of even numbers
// from all the subarrays of size K
int ans = 0;
// Generate all subarrays of size K
for (int i = 0; i <= N - M; i++)
{
// Store count of even numbers
// in current subarray of size K
int cnt = 0;
// Traverse the current subarray
for (int j = 0; j < M; j++)
{
// If current element
// is an even number
if (arr[i + j] % 2 == 0)
cnt++;
}
// Update the answer
ans = Math.Max(ans, cnt);
}
// Return answer
return ans;
}
// Driver Code
public static void Main(string[] args)
{
int []arr = { 2, 3, 5, 4, 7, 6 };
int K = 3;
// Size of the input array
int N = arr.Length;
Console.WriteLine(maxEvenIntegers(arr, N, K));
}
}
// This code is contributed by AnkThon
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the maximum count of
// even numbers from all the subarrays of
// size K
int maxEvenIntegers(int arr[], int N, int M)
{
// Stores the count of even numbers
// in a subarray of size K
int curr = 0;
// Calculate the count of even numbers
// in the current subarray
for (int i = 0; i < M; i++) {
// If current element is
// an even number
if (arr[i] % 2 == 0)
curr++;
}
// Stores the maximum count of even numbers
// from all the subarrays of size K
int ans = curr;
// Traverse remaining subarrays of size K
// using sliding window technique
for (int i = M; i < N; i++) {
// If the first element of
// the subarray is even
if (arr[i - M] % 2 == 0) {
// Update curr
curr--;
}
// If i-th element is even increment
// the count
if (arr[i] % 2 == 0)
curr++;
// Update the answer
ans = max(ans, curr);
}
// Return answer
return ans;
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 5, 4, 7, 6 };
int M = 3;
// Size of the input array
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << maxEvenIntegers(arr, N, M) << endl;
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to find the maximum count of
// even numbers from all the subarrays of
// size K
static int maxEvenIntegers(int arr[], int N, int M)
{
// Stores the count of even numbers
// in a subarray of size K
int curr = 0;
// Calculate the count of even numbers
// in the current subarray
for (int i = 0; i < M; i++)
{
// If current element is
// an even number
if (arr[i] % 2 == 0)
curr++;
}
// Stores the maximum count of even numbers
// from all the subarrays of size K
int ans = curr;
// Traverse remaining subarrays of size K
// using sliding window technique
for (int i = M; i < N; i++)
{
// If the first element of
// the subarray is even
if (arr[i - M] % 2 == 0)
{
// Update curr
curr--;
}
// If i-th element is even increment
// the count
if (arr[i] % 2 == 0)
curr++;
// Update the answer
ans = Math.max(ans, curr);
}
// Return answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 3, 5, 4, 7, 6 };
int M = 3;
// Size of the input array
int N = arr.length;
// Function call
System.out.print(maxEvenIntegers(arr, N, M) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum count of
# even numbers from all the subarrays of
# size M
def maxEvenIntegers(arr, N, M):
# Stores the count of even numbers
# in a subarray of size M
curr = 0
# Calculate the count of even numbers
# in the current subarray
for i in range(0, M):
# If current element is
# an even number
if(arr[i] % 2 == 0):
curr += 1
# Stores the maximum count of even numbers
# from all the subarrays of size M
ans = curr
# Traverse remaining subarrays of size M
# using sliding window technique
for i in range(M, N):
# If the first element of
# the subarray is even
if(arr[i - M] % 2 == 0):
# update curr
curr -= 1
# If i-th element is even increment
# the count
if(arr[i] % 2 == 0):
curr += 1
# update the answer
ans = max(curr, ans)
# Return answer
return ans
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 5, 4, 7, 6]
M = 3
# Size of the input array
N = len(arr)
# Function call
print(maxEvenIntegers(arr, N, M))
# This code is contributed by MuskanKalra1
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the maximum count of
// even numbers from all the subarrays of
// size K
static int maxEvenints(int []arr, int N, int M)
{
// Stores the count of even numbers
// in a subarray of size K
int curr = 0;
// Calculate the count of even numbers
// in the current subarray
for (int i = 0; i < M; i++)
{
// If current element is
// an even number
if (arr[i] % 2 == 0)
curr++;
}
// Stores the maximum count of even numbers
// from all the subarrays of size K
int ans = curr;
// Traverse remaining subarrays of size K
// using sliding window technique
for (int i = M; i < N; i++)
{
// If the first element of
// the subarray is even
if (arr[i - M] % 2 == 0)
{
// Update curr
curr--;
}
// If i-th element is even increment
// the count
if (arr[i] % 2 == 0)
curr++;
// Update the answer
ans = Math.Max(ans, curr);
}
// Return answer
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 2, 3, 5, 4, 7, 6 };
int M = 3;
// Size of the input array
int N = arr.Length;
// Function call
Console.Write(maxEvenints(arr, N, M) +"\n");
}
}
// This code is contributed by 29AjayKumar
输出:
2
时间复杂度: O(N * K)
辅助空间: O(1)
高效方法:可以使用滑动窗口技术来优化上述方法。请按照以下步骤解决问题:
- 初始化一个变量,例如cntMaxEven ,以将偶数的最大计数存储在大小为K的子数组中。
- 计算子数组{arr [0],…arr [K – 1]}中的偶数计数并将其存储到cntMaxEven中。
- 通过遍历[K,N – 1]范围遍历大小为K的其余子数组。对于第i次迭代,请删除子数组的第一个元素,然后将数组的当前i元素插入当前子数组。
- 计算当前子数组中的偶数,并将cntMaxEven更新为当前子数组和cntMaxEven中偶数的最大值。
- 最后,打印cntMaxEven的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the maximum count of
// even numbers from all the subarrays of
// size K
int maxEvenIntegers(int arr[], int N, int M)
{
// Stores the count of even numbers
// in a subarray of size K
int curr = 0;
// Calculate the count of even numbers
// in the current subarray
for (int i = 0; i < M; i++) {
// If current element is
// an even number
if (arr[i] % 2 == 0)
curr++;
}
// Stores the maximum count of even numbers
// from all the subarrays of size K
int ans = curr;
// Traverse remaining subarrays of size K
// using sliding window technique
for (int i = M; i < N; i++) {
// If the first element of
// the subarray is even
if (arr[i - M] % 2 == 0) {
// Update curr
curr--;
}
// If i-th element is even increment
// the count
if (arr[i] % 2 == 0)
curr++;
// Update the answer
ans = max(ans, curr);
}
// Return answer
return ans;
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 5, 4, 7, 6 };
int M = 3;
// Size of the input array
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << maxEvenIntegers(arr, N, M) << endl;
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to find the maximum count of
// even numbers from all the subarrays of
// size K
static int maxEvenIntegers(int arr[], int N, int M)
{
// Stores the count of even numbers
// in a subarray of size K
int curr = 0;
// Calculate the count of even numbers
// in the current subarray
for (int i = 0; i < M; i++)
{
// If current element is
// an even number
if (arr[i] % 2 == 0)
curr++;
}
// Stores the maximum count of even numbers
// from all the subarrays of size K
int ans = curr;
// Traverse remaining subarrays of size K
// using sliding window technique
for (int i = M; i < N; i++)
{
// If the first element of
// the subarray is even
if (arr[i - M] % 2 == 0)
{
// Update curr
curr--;
}
// If i-th element is even increment
// the count
if (arr[i] % 2 == 0)
curr++;
// Update the answer
ans = Math.max(ans, curr);
}
// Return answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 3, 5, 4, 7, 6 };
int M = 3;
// Size of the input array
int N = arr.length;
// Function call
System.out.print(maxEvenIntegers(arr, N, M) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum count of
# even numbers from all the subarrays of
# size M
def maxEvenIntegers(arr, N, M):
# Stores the count of even numbers
# in a subarray of size M
curr = 0
# Calculate the count of even numbers
# in the current subarray
for i in range(0, M):
# If current element is
# an even number
if(arr[i] % 2 == 0):
curr += 1
# Stores the maximum count of even numbers
# from all the subarrays of size M
ans = curr
# Traverse remaining subarrays of size M
# using sliding window technique
for i in range(M, N):
# If the first element of
# the subarray is even
if(arr[i - M] % 2 == 0):
# update curr
curr -= 1
# If i-th element is even increment
# the count
if(arr[i] % 2 == 0):
curr += 1
# update the answer
ans = max(curr, ans)
# Return answer
return ans
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 5, 4, 7, 6]
M = 3
# Size of the input array
N = len(arr)
# Function call
print(maxEvenIntegers(arr, N, M))
# This code is contributed by MuskanKalra1
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the maximum count of
// even numbers from all the subarrays of
// size K
static int maxEvenints(int []arr, int N, int M)
{
// Stores the count of even numbers
// in a subarray of size K
int curr = 0;
// Calculate the count of even numbers
// in the current subarray
for (int i = 0; i < M; i++)
{
// If current element is
// an even number
if (arr[i] % 2 == 0)
curr++;
}
// Stores the maximum count of even numbers
// from all the subarrays of size K
int ans = curr;
// Traverse remaining subarrays of size K
// using sliding window technique
for (int i = M; i < N; i++)
{
// If the first element of
// the subarray is even
if (arr[i - M] % 2 == 0)
{
// Update curr
curr--;
}
// If i-th element is even increment
// the count
if (arr[i] % 2 == 0)
curr++;
// Update the answer
ans = Math.Max(ans, curr);
}
// Return answer
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 2, 3, 5, 4, 7, 6 };
int M = 3;
// Size of the input array
int N = arr.Length;
// Function call
Console.Write(maxEvenints(arr, N, M) +"\n");
}
}
// This code is contributed by 29AjayKumar
输出:
2
时间复杂度: O(N)
辅助空间: O(1)