Longest subsequence of even numbers in an Array
Last Updated : 25 May, 2021
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:
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>
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1)
Similar Reads
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 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
Longest alternative parity subsequence We are given an array a of size N. The task is to print the length of the longest alternative odd/even or even/odd subsequence. Examples: Input: a[] = { 13, 16, 8, 9, 32, 10 } Output: 4 {13, 16, 9, 10} or any other subsequence of length 4 can be the answer. Input: a[] = {1, 2, 3, 3, 9} Output: 3 App
7 min read
Number of Subsequences with Even and Odd Sum Given an array, find the number of subsequences whose sum is even and the number of subsequences whose sum is odd. Example: Input: arr[] = {1, 2, 2, 3} Output: EvenSum = 7, OddSum = 8 There are 2^{N}-1 possible subsequences. The subsequences with even sum is 1) {1, 3} Sum = 4 2) {1, 2, 2, 3} Sum = 8
15 min read
Longest alternating subsequence A sequence {X1, X2, .. Xn} is an alternating sequence if its elements satisfy one of the following relations : X1 < X2 > X3 < X4 > X5 < â¦. xn or X1 > X2 < X3 > X4 < X5 > â¦. xn Examples: Input: arr[] = {1, 5, 4}Output: 3Explanation: The whole arrays is of the form x1
15+ min read