📌  相关文章
📜  数组中偶数的最长子序列

📅  最后修改于: 2021-05-17 21:29:03             🧑  作者: Mango

给定一个包含N个整数的数组arr [] ,任务是打印数组中偶数的最长子序列的长度。

例子:

方法:需要从数组中得出的一个观察结果是,仅包含偶数的最长子序列将是所有偶数的总计数。因此,请按照以下步骤计算答案:

  • 逐元素遍历给定的数组。
  • 如果元素是偶数,则增加结果子序列的长度。
  • 遍历完成后,将仅包含偶数的最长子序列的所需长度存储在计数器中。

下面是上述方法的实现:

C++
// C++ program to find the length of the
// longest subsequence which contains
// all even numbers
  
#include 
using namespace std;
#define N 100000
  
// Function to find the length of the
// longest subsequence
// which contain all even numbers
int longestEvenSubsequence(int arr[], int n)
{
    // Counter to store the
    // length of the subsequence
    int answer = 0;
  
    // Iterating through the array
    for (int i = 0; i < n; i++) {
  
        // Checking if the element is
        // even or not
        if (arr[i] % 2 == 0) {
  
            // If it is even, then
            // increment the counter
            answer++;
        }
    }
  
    return answer;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 4, 11, 2, 9, 21 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << longestEvenSubsequence(arr, n)
         << endl;
  
    return 0;
}


Java
// Java program to find the length of the
// longest subsequence which contains
// all even numbers
import java.util.*;
class GFG{
  
// Function to find the length of the
// longest subsequence
// which contain all even numbers
static int longestEvenSubsequence(int arr[], int n)
{
    // Counter to store the
    // length of the subsequence
    int answer = 0;
  
    // Iterating through the array
    for (int i = 0; i < n; i++) 
    {
  
        // Checking if the element is
        // even or not
        if (arr[i] % 2 == 0) 
        {
  
            // If it is even, then
            // increment the counter
            answer++;
        }
    }
    return answer;
}
  
// Driver code
public static void main(String args[])
{
    int []arr = { 3, 4, 11, 2, 9, 21 };
    int n = arr.length;
  
    System.out.print(longestEvenSubsequence(arr, n));
}
}
  
// This code is contributed by Akanksha_Rai


Python3
# Python3 program to find the length
# of the longest subsequence which 
# contains all even numbers
  
N = 100000
  
# Function to find the length 
# of the longest subsequence
# which contain all even numbers
def longestEvenSubsequence(arr, n):
  
    # Counter to store the
    # length of the subsequence
    answer = 0
      
    # Iterating through the array
    for i in range(n):
  
        # Checking if the element
        # is even or not
        if (arr[i] % 2 == 0):
  
            # If it is even, then
            # increment the counter
            answer += 1
          
    return answer
  
# Driver code
if __name__ == "__main__":
      
    arr = [ 3, 4, 11, 2, 9, 21 ]
    n = len(arr)
  
    print(longestEvenSubsequence(arr, n))
  
# This code is contributed by chitranayal


C#
// C# program to find the length of the
// longest subsequence which contains
// all even numbers
using System;
class GFG{
  
// Function to find the length of the
// longest subsequence
// which contain all even numbers
static int longestEvenSubsequence(int []arr, int n)
{
    // Counter to store the
    // length of the subsequence
    int answer = 0;
  
    // Iterating through the array
    for (int i = 0; i < n; i++) 
    {
  
        // Checking if the element is
        // even or not
        if (arr[i] % 2 == 0) 
        {
  
            // If it is even, then
            // increment the counter
            answer++;
        }
    }
    return answer;
}
  
// Driver code
public static void Main()
{
    int []arr = { 3, 4, 11, 2, 9, 21 };
    int n = arr.Length;
  
    Console.WriteLine(longestEvenSubsequence(arr, n));
}
}
  
// This code is contributed by Nidhi_Biet


输出:
2

时间复杂度: O(N) ,其中N是数组的长度。
辅助空间: O(1)