Length of the longest subsequence consisting of distinct elements
Last Updated :
21 May, 2024
Given an array arr[] of size N, the task is to find the length of the longest subsequence consisting of distinct elements only.
Examples:
Input: arr[] = {1, 1, 2, 2, 2, 3, 3}
Output: 3
Explanation:
The longest subsequence with distinct elements is {1, 2, 3}
Input: arr[] = { 1, 2, 3, 3, 4, 5, 5, 5 }
Output: 5
Naive Approach: The simplest approach is to generate all the subsequences of the array and check if it consists of only distinct elements or not. Keep updating the maximum length of such subsequences obtained. Finally, print the maximum length obtained.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The length of the longest subsequence containing only distinct elements will be equal to the count of distinct elements in the array. Follow the steps below to solve the problem:
- Traverse the given array keep inserting encountered elements in a Hashset.
- Since HashSet consists of only unique elements, print the size of the HashSet as the required answer after completing the traversal of the array.
Below is the implementation of the above approach:
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
Python
# 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
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>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Length of longest subsequence consisting of distinct adjacent elements Given an array arr[], the task is to find the length of the longest subsequence of the array arr[] such that all adjacent elements in the subsequence are different. Examples: Input: arr[] = {4, 2, 3, 4, 3}Output: 5Explanation:The longest subsequence where no two adjacent elements are equal is {4, 2,
5 min read
Count of Subsequences with distinct elements Given an array arr[] (1<=a[i]<=1e9) containing N (1<=N<=1e5) elements, the task is to find the total number of subsequences such that all elements in that subsequences are distinct. Since the answer can be very large print and modulo 1e9+7. Examples: Input: arr[] = [1, 1, 2, 2]Output: 8E
5 min read
Find length of the longest non-intersecting anagram Subsequence Given a string S of length N, find the length of the two longest non-intersecting subsequences in S that are anagrams of each other. Input: S = "aaababcd"Output: 3Explanation: Index of characters in the 2 subsequences are: {0, 1, 3} = {a, a, b} {2, 4, 5} = {a, a, b} The above two subsequences of S a
6 min read
Count of subsequences having maximum distinct elements Given an arr of size n. The problem is to count all the subsequences having maximum number of distinct elements. Examples: Input : arr[] = {4, 7, 6, 7} Output : 2 The indexes for the subsequences are: {0, 1, 2} - Subsequence is {4, 7, 6} and {0, 2, 3} - Subsequence is {4, 6, 7} Input : arr[] = {9, 6
5 min read
Longest Subsequence with at least one common digit in every element Given an array. The task is to find the length of the longest subsequence in which all elements must have at least one digit in common. Examples: Input : arr[] = { 11, 12, 23, 74, 13 } Output : 3 Explanation: The elements 11, 12, and 13 have the digit '1' as common. So it is the required longest sub
9 min read