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 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:
- 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 is completed, 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 numbersint 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 codeint 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 numbersimport java.util.*;class GFG{// Function to find the length of the// longest subsequence// which contain all even numbersstatic 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 codepublic 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 numbersN = 100000# Function to find the length # of the longest subsequence# which contain all even numbersdef 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 codeif __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 numbersusing System;class GFG{// Function to find the length of the// longest subsequence// which contain all even numbersstatic 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 codepublic 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 numberslet N = 100000;// Function to find the length of the// longest subsequence// which contain all even numbersfunction 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 codelet 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> |
2
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
