Dada una array arr[] que contiene N enteros, la tarea es imprimir la longitud de la subsecuencia más larga de números pares en la array.
Ejemplos:
Entrada: arr[] = {3, 4, 11, 2, 9, 21}
Salida: 2
Explicación:
La subsecuencia más larga que contiene números pares es {4, 2}.
Por lo tanto, la respuesta es 2.Entrada: arr[] = {6, 4, 10, 13, 9, 25}
Salida: 3
La subsecuencia más larga que contiene números pares es {6, 4, 10}.
Por lo tanto, la respuesta es 3.
Enfoque: una observación que debe hacerse a partir de la array es que la subsecuencia más larga que contiene solo un número par será el recuento total de todos los números pares. Por lo tanto, se siguen los siguientes pasos para calcular la respuesta:
- Atraviesa la array dada elemento por elemento.
- Si el elemento es un número par , aumente la longitud de la subsecuencia resultante.
- Cuando se completa el recorrido, la longitud requerida de la subsecuencia más larga que contiene solo números pares se almacena en el contador.
A continuación se muestra la implementación del enfoque anterior:
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>
2
Complejidad de tiempo: O(N) , donde N es la longitud de la array.
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por muskan_garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA