📜  最长交替偶奇数子数组的长度

📅  最后修改于: 2021-04-21 22:29:42             🧑  作者: Mango

给定一个由n个整数组成的数组a [] ,任务是找到该数组中存在的最长的交替偶数奇数子数组的长度。
例子:

方法:为解决上述问题,我们必须观察到两个偶数之和为偶数,两个奇数之和为偶数,但一个偶数和一个奇数之和为奇数。

  • 最初初始化cnt一个计数器以将长度存储为1。
  • 在数组元素之间进行迭代,检查连续的元素是否具有奇数和。
  • 如果cnt为奇数,则将cnt增加1。
  • 如果没有奇数和,则用1重新初始化cnt。

下面是上述方法的实现:

C++
// C++ program to find the Length of the
// longest alternating even odd subarray
 
#include 
using namespace std;
 
// Function to find the longest subarray
int longestEvenOddSubarray(int a[], int n)
{
    // Length of longest
    // alternating subarray
    int longest = 1;
    int cnt = 1;
 
    // Iterate in the array
    for (int i = 0; i < n - 1; i++) {
 
        // increment count if consecutive
        // elements has an odd sum
        if ((a[i] + a[i + 1]) % 2 == 1) {
            cnt++;
        }
        else {
            // Store maximum count in longest
            longest = max(longest, cnt);
 
            // Reinitialize cnt as 1 consecutive
            // elements does not have an odd sum
            cnt = 1;
        }
    }
 
    // Length of 'longest' can never be 1
    // since even odd has to occur in pair or more
    // so return 0 if longest = 1
    if (longest == 1)
        return 0;
 
    return max(cnt, longest);
}
 
/* Driver code*/
int main()
{
    int a[] = { 1, 2, 3, 4, 5, 7, 8 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << longestEvenOddSubarray(a, n);
    return 0;
}


Java
// Java program to find the Length of the
// longest alternating even odd subarray
import java.util.*;
class GFG{
     
// Function to find the longest subarray
static int longestEvenOddSubarray(int a[], int n)
{
    // Length of longest
    // alternating subarray
    int longest = 1;
    int cnt = 1;
 
    // Iterate in the array
    for (int i = 0; i < n - 1; i++)
    {
 
        // increment count if consecutive
        // elements has an odd sum
        if ((a[i] + a[i + 1]) % 2 == 1)
        {
            cnt++;
        }
        else
        {
            // Store maximum count in longest
            longest = Math.max(longest, cnt);
 
            // Reinitialize cnt as 1 consecutive
            // elements does not have an odd sum
            cnt = 1;
        }
    }
 
    // Length of 'longest' can never be 1
    // since even odd has to occur in pair or more
    // so return 0 if longest = 1
    if (longest == 1)
        return 0;
 
    return Math.max(cnt, longest);
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 2, 3, 4, 5, 7, 8 };
 
    int n = a.length;
 
    System.out.println(longestEvenOddSubarray(a, n));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to find the length of the
# longest alternating even odd subarray
 
# Function to find the longest subarray
def longestEvenOddSubarray(arr, n):
     
    # Length of longest
    # alternating subarray
    longest = 1
    cnt = 1
     
    # Iterate in the array
    for i in range(n - 1):
         
        # Increment count if consecutive
        # elements has an odd sum
        if((arr[i] + arr[i + 1]) % 2 == 1):
            cnt = cnt + 1
        else:
             
            # Store maximum count in longest
            longest = max(longest, cnt)
             
            # Reinitialize cnt as 1 consecutive
            # elements does not have an odd sum
            cnt = 1
             
    # Length of 'longest' can never be 1 since
    # even odd has to occur in pair or more
    # so return 0 if longest = 1
    if(longest == 1):
       return 0
         
    return max(cnt, longest)
 
# Driver Code
arr = [ 1, 2, 3, 4, 5, 7, 8 ]
n = len(arr)
 
print(longestEvenOddSubarray(arr, n))
 
# This code is contributed by skylags


C#
// C# program to find the Length of the
// longest alternating even odd subarray
using System;
 
class GFG{
     
// Function to find the longest subarray
static int longestEvenOddSubarray(int[] a, int n)
{
     
    // Length of longest
    // alternating subarray
    int longest = 1;
    int cnt = 1;
     
    // Iterate in the array
    for(int i = 0; i < n - 1; i++)
    {
         
       // Increment count if consecutive
       // elements has an odd sum
       if ((a[i] + a[i + 1]) % 2 == 1)
       {
           cnt++;
       }
       else
       {
            
           // Store maximum count in longest
           longest = Math.Max(longest, cnt);
            
           // Reinitialize cnt as 1 consecutive
           // elements does not have an odd sum
           cnt = 1;
       }
    }
     
    // Length of 'longest' can never be 1
    // since even odd has to occur in pair
    // or more so return 0 if longest = 1
    if (longest == 1)
        return 0;
    return Math.Max(cnt, longest);
}
 
// Driver code
static void Main()
{
         
    int[] a = { 1, 2, 3, 4, 5, 7, 8 };
    int n = a.Length;
         
    Console.WriteLine(longestEvenOddSubarray(a, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
5