Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Longest subsequence having equal numbers of 0 and 1
Next article icon

Longest subsequence of even numbers in an Array

Last Updated : 25 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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



Next Article
Longest subsequence having equal numbers of 0 and 1

M

muskan_garg
Improve
Article Tags :
  • Arrays
  • DSA
  • School Programming
  • Write From Home
  • subsequence
Practice Tags :
  • Arrays

Similar Reads

  • Length of Longest Perfect number Subsequence in an Array
    Given an array arr[] containing non-negative integers of length N, the task is to print the length of the longest subsequence of the Perfect number in the array. A number is a perfect number if it is equal to the sum of its proper divisors, that is, the sum of its positive divisors excluding the num
    7 min read
  • Length of longest subsequence of Fibonacci Numbers in an Array
    Given an array arr containing non-negative integers, the task is to print the length of the longest subsequence of Fibonacci numbers in this array.Examples: Input: arr[] = { 3, 4, 11, 2, 9, 21 } Output: 3 Here, the subsequence is {3, 2, 21} and hence the answer is 3.Input: arr[] = { 6, 4, 10, 13, 9,
    5 min read
  • Longest subsequence having equal numbers of 0 and 1
    Given a binary array, the task is to find the size of the largest sub_sequence which having equal number of zeros and one. Examples : Input : arr[] = { 1, 0, 0, 1, 0, 0, 0, 1 } Output: 6 Input : arr[] = { 0, 0, 1, 1, 1, 1, 1, 0, 0 }Output : 8 simple solution is that we generate all possible sub_sequ
    11 min read
  • Print all Longest dividing subsequence in an Array
    Given an array arr[] of N integers, the task is to print all the element of Longest Dividing Subsequence formed by the given array. If we have more than one sequence with maximum length then print all of them. Examples: Input: arr[] = { 2, 11, 16, 12, 36, 60, 71 } Output: 2 12 36 2 12 60 Explanation
    8 min read
  • Length of longest subsequence in an Array having all elements as Nude Numbers
    Given an array arr[] of N positive integers, the task is to print the length of the longest subsequence of the array such that all of its elements are Nude Numbers. Examples: Input: arr[] = {34, 34, 2, 2, 3, 333, 221, 32 }Output: 4Explanation:Longest Nude number subsequence is {2, 2, 3, 333} and hen
    11 min read
  • Longest sequence of positive integers in an array
    Find the longest-running positive sequence in an array. Examples: Input : arr[] = {1, 2, -3, 2, 3, 4, -6, 1, 2, 3, 4, 5, -8, 5, 6} Output :Index : 7, length : 5 Input : arr[] = {-3, -6, -1, -3, -8} Output : No positive sequence detected. A simple solution is to use two nested loops. In the outer loo
    7 min read
  • Longest Subsequence where index of next element is arr[arr[i] + i]
    Given an array arr[], the task is to find the maximum length sub-sequence from the array which satisfy the following condition: Any element can be chosen as the first element of the sub-sequence but the index of the next element will be determined by arr[arr[i] + i] where i is the index of the previ
    6 min read
  • Longest Increasing Odd Even Subsequence
    Given an array of size n. The problem is to find the length of the subsequence in the given array such that all the elements of the subsequence are sorted in increasing order and also they are alternately odd and even. Note that the subsequence could start either with the odd number or with the even
    8 min read
  • Find the longest sub-array having exactly k odd numbers
    Given an array of size n. The problem is to find the longest sub-array having exactly k odd numbers. Examples: Input : arr[] = {2, 3, 4, 11, 4, 12, 7}, k = 1 Output : 4 The sub-array is {4, 11, 4, 12}. Input : arr[] = {3, 4, 6, 1, 9, 8, 2, 10}, k = 2 Output : 7 The sub-array is {4, 6, 1, 9, 8, 2, 10
    7 min read
  • Length of the longest ZigZag Subsequence of the given Array
    Given an array arr[] of integers, if the differences between consecutive numbers alternate between positive and negative. More formally, if arr[i] - arr[i-1] has a different sign for all i from 1 to n-1, the subsequence is considered a zig-zag subsequence. Find out the length of the longest Zig-Zag
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences