Dada una array arr[] de tamaño N , la tarea es encontrar la longitud de la subsecuencia más larga que consiste únicamente en elementos distintos.
Ejemplos:
Entrada: arr[] = {1, 1, 2, 2, 2, 3, 3}
Salida: 3
Explicación:
La subsecuencia más larga con elementos distintos es {1, 2, 3}
Entrada: arr[] = { 1, 2 , 3, 3, 4, 5, 5, 5 }
Salida: 5
Enfoque ingenuo: el enfoque más simple es generar todas las subsecuencias de la array y verificar si consta solo de elementos distintos o no. Seguir actualizando la longitud máxima de dichas subsecuencias obtenidas. Finalmente, imprima la longitud máxima obtenida.
Complejidad temporal: O(2 N )
Espacio auxiliar: O(1)
Enfoque eficiente: la longitud de la subsecuencia más larga que contiene solo elementos distintos será igual al recuento de elementos distintos en la array. Siga los pasos a continuación para resolver el problema:
- Atraviese la array dada, siga insertando elementos encontrados en un Hashset .
- Dado que HashSet consta solo de elementos únicos, imprima el tamaño de HashSet como la respuesta requerida después de completar el recorrido de la array.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find length of // the longest subsequence // consisting of distinct elements int longestSubseq(int arr[], int n) { // Stores the distinct // array elements unordered_set<int> s; // Traverse the input array for (int i = 0; i < n; i++) { // If current element has not // occurred previously if (s.find(arr[i]) == s.end()) { // Insert it into set s.insert(arr[i]); } } return s.size(); } // Driver Code int main() { // Given array int arr[] = { 1, 2, 3, 3, 4, 5, 5, 5 }; int n = sizeof(arr) / sizeof(arr[0]); // Function Call cout << longestSubseq(arr, n); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find length of // the longest subsequence // consisting of distinct elements static int longestSubseq(int arr[], int n) { // Stores the distinct // array elements Set<Integer> s = new HashSet<>(); // Traverse the input array for(int i = 0; i < n; i++) { // If current element has not // occurred previously if (!s.contains(arr[i])) { // Insert it into set s.add(arr[i]); } } return s.size(); } // Driver code public static void main (String[] args) { // Given array int arr[] = { 1, 2, 3, 3, 4, 5, 5, 5 }; int n = arr.length; // Function call System.out.println(longestSubseq(arr, n)); } } // This code is contributed by offbeat
Python3
# Python3 program for # the above approach # Function to find length of # the longest subsequence # consisting of distinct elements def longestSubseq(arr, n): # Stores the distinct # array elements s = set() # Traverse the input array for i in range(n): # If current element has not # occurred previously if (arr[i] not in s): # Insert it into set s.add(arr[i]) return len(s) # Given array arr = [1, 2, 3, 3, 4, 5, 5, 5] n = len(arr) # Function Call print(longestSubseq(arr, n)) # This code is contributed by divyeshrabadiya07
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find length of // the longest subsequence // consisting of distinct elements static int longestSubseq(int []arr, int n) { // Stores the distinct // array elements HashSet<int> s = new HashSet<int>(); // Traverse the input array for(int i = 0; i < n; i++) { // If current element has not // occurred previously if (!s.Contains(arr[i])) { // Insert it into set s.Add(arr[i]); } } return s.Count; } // Driver code public static void Main(string[] args) { // Given array int []arr = { 1, 2, 3, 3, 4, 5, 5, 5 }; int n = arr.Length; // Function call Console.Write(longestSubseq(arr, n)); } } // This code is contributed by rutvik_56
C++
#include <iostream> using namespace std; int main() { cout<<"GFG!"; return 0; }
Javascript
<script> // Javascript program for the above approach // Function to find length of // the longest subsequence // consisting of distinct elements function longestSubseq(arr, n) { // Stores the distinct // array elements var s = new Set(); // Traverse the input array for (var i = 0; i < n; i++) { // If current element has not // occurred previously if (s.has(arr[i]) == false) { // Insert it into set s.add(arr[i]); } } return s.size; } // Driver Code // Given array var arr = [ 1, 2, 3, 3, 4, 5, 5, 5 ]; var n = arr.length; // Function Call document.write(longestSubseq(arr, n)); // This code is contributed by ShubhamSingh10 </script>
5
Complejidad temporal: O(N)
Espacio auxiliar: O(N)