给定一个数组arr []。任务是找到arr []的最长子数组的长度,使其仅包含偶数元素。
例子:
Input : arr[] = { 5, 2, 4, 7 }
Output : Length = 2
subArr[] = {2, 4}
Input : arr[] = {9, 8, 5, 4, 4, 4, 2, 4, 1}
Output : Length 5
subArr[] = {4, 4, 4, 2, 4}
这样做的目的是观察到只有偶数元素的最大子数组是数组中连续偶数元素的最大数量。因此,现在该任务减少了,以查找阵列中连续的偶数元素的最大数量。
为此,请使用ans和current_count这两个变量遍历数组。变量ans存储最终答案,而current_count存储仅包含偶数的子数组的长度。
现在,每当偶数元素被发现,不断递增的CURRENT_COUNT,每当一个奇怪的元素被找到的最大ANS和CURRENT_COUNT和复位CURRENT_COUNT的花费为零。
最后, ans将存储仅包含偶数元素的最大子数组的长度。
下面是上述方法的实现:
C++
// C++ Program to find the Length of the
// largest Subarray with only even elements
#include
#include
using namespace std;
// Function to find the Length of the
// largest Subarray with only even elements
int maxEvenSubarray(int array[], int N)
{
int ans = 0;
int count = 0;
// Iterate the loop
for (int i = 0; i < N; i++) {
// Check whether the element is
// even in continuous fashion
if (array[i] % 2 == 0) {
count++;
ans = max(ans, count);
}
else {
// If element are not even in continuous
// fashion, Reinitialize the count
count = 0;
}
}
// Check for the last element in the array
ans = max(ans, count);
return ans;
}
// Driver Code
int main()
{
int arr[] = { 9, 8, 5, 4, 4, 4, 2, 4, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maxEvenSubarray(arr, N);
return 0;
}
Java
// Java Program to find the Length of the longest
// Subarray with only Even Elements
public class GFG {
// Function to find the Length of the longest
// Subarray with only Even Elements
static int maxEvenSubarray(int array[], int N)
{
int ans = 0;
int count = 0;
// Iterate the loop
for (int i = 0; i < array.length; i++) {
// Check whether the element is
// even in continuous fashion
if (array[i] % 2 == 0) {
count++;
ans = Math.max(ans, count);
}
else {
// If element are not even in continuous
// fashion. Reinitialize the count
count = 0;
}
}
// Check for the last element in the array
ans = Math.max(ans, count);
return ans;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 9, 8, 5, 4, 4, 4, 2, 4, 1 };
int N = arr.length;
System.out.println(maxEvenSubarray(arr, N));
}
}
Python3
# Python3 Program to find the Length of the
# largest Subarray with only even elements
# Function to find the Length of the
# largest Subarray with only even elements
def maxEvenSubarray(array,N):
ans = 0
count = 0
# Iterate the loop
for i in range(0,N):
# Check whether the element is
# even in continuous fashion
if array[i]%2==0:
count +=1
ans = max(ans,count)
else:
# If element are not even in continuous
# fashion, Reinitialize the count
count = 0
# Check for the last element in the array
ans = max(ans,count)
return ans
# Driver code
if __name__=='__main__':
arr = [9, 8, 5, 4, 4, 4, 2, 4, 1]
N = len(arr)
print(maxEvenSubarray(arr,N))
# This article is contributed by Shrikant13
C#
// C# Program to find the Length
// of the largest Subarray with
// only even elements
using System;
class GFG
{
// Function to find the Length
// of the largest Subarray with
// only even elements
static int maxEvenSubarray(int []array,
int N)
{
int ans = 0;
int count = 0;
// Iterate the loop
for (int i = 0; i < N; i++)
{
// Check whether the element is
// even in continuous fashion
if (array[i] % 2 == 0)
{
count++;
ans = Math.Max(ans, count);
}
else
{
// If element are not even in
// continuous fashion,
// Reinitialize the count
count = 0;
}
}
// Check for the last
// element in the array
ans = Math.Max(ans, count);
return ans;
}
// Driver Code
public static void Main()
{
int []arr = { 9, 8, 5, 4,
4, 4, 2, 4, 1 };
int N = arr.Length;
Console.WriteLine(maxEvenSubarray(arr, N));
}
}
// This code is contributed by ihritik
PHP
输出:
5
时间复杂度:
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。