Sunday, November 17, 2024
Google search engine
HomeData Modelling & AILongest subsequence of even numbers in an Array

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:
Explanation: 
The longest subsequence containing even numbers is {4, 2}. 
Hence, the answer is 2.

Input: arr[] = {6, 4, 10, 13, 9, 25} 
Output:
The longest subsequence containing even numbers is {6, 4, 10}. 
Hence, the answer is 3.  

Approach: One observation that needs to be made from the array is that the longest subsequence containing only an 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++




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


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


Javascript




<script>
// Javascript program to find the length of the
// longest subsequence which contains
// all even numbers
 
let N = 100000;
 
// Function to find the length of the
// longest subsequence
// which contain all even numbers
function longestEvenSubsequence(arr, n) {
    // Counter to store the
    // length of the subsequence
    let answer = 0;
 
    // Iterating through the array
    for (let 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
let arr = [3, 4, 11, 2, 9, 21];
let n = arr.length;
 
document.write(longestEvenSubsequence(arr, n) + "<br>");
 
// This code is contributed by _saurabh_jaiswal
</script>


Output: 

2

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments