给定一个大小为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
Javascript
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
Javascript
输出:
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
蟒蛇3
# 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
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live