Longest consecutive subsequence | Practice | GeeksforGeeks

Given an array of positive integers. Find the length of the longest sub-sequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order.
 
Example 1:
Input:
N = 7
a[] = {2,6,1,9,4,5,3}
Output:
6
Explanation:
The consecutive numbers here
are 1, 2, 3, 4, 5, 6. These 6 
numbers form the longest consecutive
subsquence.
Example 2:
Input:
N = 7
a[] = {1,9,3,10,4,20,2}
Output:
4
Explanation:
1, 2, 3, 4 is the longest
consecutive subsequence.


Your Task:
You don't need to read input or print anything. Your task is to complete the function findLongestConseqSubseq() which takes the array arr[] and the size of the array as inputs and returns the length of the longest subsequence of consecutive integers. 

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

Constraints:
1 <= N <= 105
0 <= a[i] <= 105
0
singhnilesh03018 hours ago
//simple dp solution
class Solution:   def findLongestConseqSubseq(self,arr, N):       c=0       s=max(arr)+1       ans=0       t=[0]*s       for i in range(N):           t[arr[i]]+=1       for j in range(len(t)):           if(t[j]>0):               c+=1           else:               c=0           ans=max(ans,c)       return ans
0
bhardwajji2 days ago
C++
int findLongestConseqSubseq(int arr[], int n)
    {
        set<int>s;
        for(int i=0;i<n;i++){
            s.insert(arr[i]);
        }
        int ans=INT_MIN;
        int temp=0;
        
        for(auto it : s){
            auto x = s.upper_bound(it);
            if(it+1 == *x){
                temp++;
            }
            else 
                temp=0;
            ans = max(temp,ans);
            
        }
        
        return ans+1;
        
    }
 
0
shubham211019972 days ago
static int findLongestConseqSubseq(int arr[], int N)
{
  /* Naive Approach
  Arrays.sort(arr);
   int count=1,res=1;
   for(int i=1;i<N;i++){
       if(i>0 && arr[i]==arr[i-1]) continue;
       if(arr[i]==(arr[i-1]+1)){
           count++;
           res=Math.max(res,count);
       }else{
           count=1;
       }
   }
   return res;*/
   HashSet<Integer>h=new HashSet<>();
   for(int i:arr){
       h.add(i);
   }
   int res=1;
   for(Integer i:h){
       if(h.contains(i-1)==false){
           int count=1;
           while(h.contains(i+count)){
               count++;
               res=Math.max(res,count);
           }
            
       }
   }
   return res;
}
0
niravramotiya0032 days ago
def findLongestConseqSubseq(self,arr, N):
       arr.sort()
       count=0
       maxy=0
       for x in range(len(arr)-1):
           if arr[x]!=arr[x+1]:
               if arr[x]+1 ==arr[x+1]:
                   count+=1
                   if maxy<count:
                       maxy=count
               else:
                   count=0
       return maxy+1
           
0
shivanshpandey1573 days ago
 int findLongestConseqSubseq(int arr[], int n)   {     //Your code here     int pre[100000]={0};     for(int i=0;i<n;i++)     {         pre[arr[i]]=1;     }     int count=1,ans=1;;     for(int i=1;i<=100000;i++)     {         if(pre[i]&&pre[i+1])         count++;         else         {             ans=max(ans,count);             count=1;         }     }     ans=max(ans,count);     return ans;   }
-1
arya049444 days ago
 int findLongestConseqSubseq(int arr[], int N)
    {
      //Your code here
      unordered_set<int> us;
      int mn=INT_MAX,mx=0;
      for(auto i=0;i<N;i++){
         mn=min(mn,arr[i]);
         mx=max(mx,arr[i]);
         us.insert(arr[i]);
      }
      //bl=bestlength l=length
     int bl=1;
     int l=1;
     while(mn<mx){
         if(us.find(mn)==us.end()){
             mn++;
             continue;
         }
        if(us.find(mn+1)!=us.end()){
            l++;
            bl=max(l,bl);
            mn=mn+1;
        }else{
            l=1;
           mn=mn+1;
        }
     }
     return bl;
    }
0
rahuldc9994 days ago
JAVA O(N) Solution static int findLongestConseqSubseq(int arr[], int N){   // add your code here   Set<Integer> arrSet= new HashSet<Integer>();   for(int i=0;i<N;i++){       arrSet.add(arr[i]);   }      int max=0;      for(int i=0;i<N;i++){     int currentMax=0;      if(!arrSet.contains(arr[i]-1)){         int j=arr[i];     while(arrSet.contains(j++)){       currentMax++;       }      }else     {         currentMax=max;     }     max=Math.max(max,currentMax);          }   return max;    }
0
sudhanshusingh09075 days ago
int dp[100005]={0};
        for(int i=0;i<N;i++){
            dp[arr[i]]++;
        }
        int maxi=0;
        int count=0;
        for(int i=0;i<=100005;i++){
            if(dp[i]>0)
                count++;
            else
                count=0;
            maxi=max(maxi,count);
        }
        return maxi;
+1
atreyrachit231 week ago
class Solution{ public:   // arr[] : the input array   // N : size of the array arr[]      //Function to return length of longest subsequence of consecutive integers.   int findLongestConseqSubseq(int arr[], int N)   {  unordered_map<int,int>s;  int mxele=0;  for(int i=0;i<N;i++)  {      if(s.find(arr[i])==s.end())      s[arr[i]]=1;      mxele=max(mxele,arr[i]);        }  int mx=0;  int tmp=0;  for(int i=1;i<=mxele;i++)  {      if(s.find(i)!=s.end())      tmp++;      else      tmp=0;        mx=max(mx,tmp);  }  return mx;   }};
-1
ahadfardeen1 week ago
JAVA EASY SOLUTION
FIRST OF ALL SORT THE ARRAY
THEN CHECK WHETHER THE ELEMENT WHICH IS ON THE RIGHT OF THE GIVEN INDEX ELEMENT IS JUST GREATER THAN THE CURRENT ELEMENT, MAINTAIN A VARIABLE WHICH IS CHECKING WHETHER OUR COUNT VALUE REACHES THE VALUE GREATER THAN THE MAXIMUM VALUE.
 
 Arrays.sort(arr);
   int count=0;
   int max=0;
   for(int i=0;i<N-1;i++){
       if(arr[i+1]-arr[i]==1){
           count++;
       }
       else if(arr[i+1]-arr[i]==0){
           continue;
       }
       else {
           if(count>max){
               max=count;
           }
           count=0;
       }
   }
   return Math.max(count,max)+1;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.