Given an array **arr[]** containing **N** integers, the task is to print the length of the longest subsequence of even numbers in the array.

**Examples:**

Input:arr[] = {3, 4, 11, 2, 9, 21}

Output:2

Explanation:

The longest subsequence containing even numbers is {4, 2}.

Hence the answer is 2.

Input:arr[] = {6, 4, 10, 13, 9, 25}

Output:3

The longest subsequence containing even numbers is {6, 4, 10}.

Hence the answer is 3.

**Approach:** One observation which needs to be made from the array is that the longest subsequence containing only even number will be the total count of all the even numbers. Therefore, the following steps are followed to compute the answer:

- Traverse through the given array element by element.
- If the element is an even number, then increase the length of the resulting subsequence.
- When the traversal completes, the required length of the longest subsequence containing only even numbers is stored in the counter.

Below is the implementation of the above approach:

## C++

`// C++ program to find the length of the ` `// longest subsequence which contains ` `// all even numbers ` ` ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** *O(N)*, where N is the length of the array.

**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.