Longest subsequence of even numbers in an Array

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:



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

2

Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.