给定N个元素的数组arr [] 。任务是找到给定数组中连续偶数的最大数量。
例子:
Input: arr[] = {1, 2, 3, 4, 6, 7}
Output: 2
Maximum contiguous even number sequence is {4, 6}
Input: arr[] = {1, 0, 2, 4, 3, 8, 9}
Output: 3
Maximum contiguous even number sequence is {0, 2, 4}
方法:想法是继续使用两个名为current_max和max_so_far的计数变量遍历数组。如果找到偶数,则增加current_max并将其与max_so_far进行比较。每次发现奇数元素时,将current_max重置为0。
下面是上述方法的实现:
C++
// CPP program to count maximum
// contiguous even numbers
#include
using namespace std;
// Function to count maximum
// contiguous even numbers
int countMaxContiguous(int arr[], int n)
{
int current_max = 0, max_so_far = 0;
for (int i = 0; i < n; i++) {
// If current element is not even
// reset current_max
if (arr[i] % 2 != 0)
current_max = 0;
// If even element is found, increment
// current_max and update result if
// count becomes more
else {
current_max++; // increase count
max_so_far = max(current_max, max_so_far);
}
}
return max_so_far;
}
// Driver code
int main()
{
int arr[] = { 1, 0, 2, 4, 3, 8, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countMaxContiguous(arr, n);
return 0;
}
Java
// Java program to count maximum
// contiguous even numbers
import java.io.*;
class GFG
{
// Function to count maximum
// contiguous even numbers
static int countMaxContiguous(int arr[], int n)
{
int current_max = 0, max_so_far = 0;
for (int i = 0; i < n; i++)
{
// If current element is not
// even reset current_max
if (arr[i] % 2 != 0)
current_max = 0;
// If even element is found, increment
// current_max and update result if
// count becomes more
else
{
current_max++; // increase count
max_so_far = Math.max(current_max,
max_so_far);
}
}
return max_so_far;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 1, 0, 2, 4, 3, 8, 9 };
int n = arr.length;
System.out.println(countMaxContiguous(arr, n));
}
}
// This code is contributed
// by inder_verma
Python 3
# Python 3 program to count maximum
# contiguous even numbers
# Function to count maximum
# contiguous even numbers
def countMaxContiguous(arr, n):
current_max = 0
max_so_far = 0
for i in range(n) :
# If current element is not
# even reset current_max
if (arr[i] % 2 != 0):
current_max = 0
# If even element is found,
# increment current_max and
# update result if count
# becomes more
else :
current_max += 1 # increase count
max_so_far = max(current_max,
max_so_far)
return max_so_far
# Driver code
if __name__ == "__main__":
arr = [ 1, 0, 2, 4, 3, 8, 9 ]
n = len(arr)
print(countMaxContiguous(arr, n))
# This code is contributed
# by ChitraNayal
C#
// C# program to count maximum
// contiguous even numbers
using System;
class GFG
{
// Function to count maximum
// contiguous even numbers
static int countMaxContiguous(int []arr, int n)
{
int current_max = 0, max_so_far = 0;
for (int i = 0; i < n; i++)
{
// If current element is not
// even reset current_max
if (arr[i] % 2 != 0)
current_max = 0;
// If even element is found, increment
// current_max and update result if
// count becomes more
else
{
current_max++; // increase count
max_so_far = Math.Max(current_max,
max_so_far);
}
}
return max_so_far;
}
// Driver code
public static void Main ()
{
int []arr = { 1, 0, 2, 4, 3, 8, 9 };
int n = arr.Length;
Console.WriteLine(countMaxContiguous(arr, n));
}
}
// This code is contributed
// by inder_verma
PHP
Javascript
输出:
3
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。