Longest consecutive subsequence

question–>
https://practice.geeksforgeeks.org/problems/longest-consecutive-subsequence2449/1#
code–>


not passing all testcases

Hey @sheikhhaji18

       for(int i=0;i<n;i++){
           int no=arr[i];
           if(m[no])continue;//to make sure not processed again
           if(m.count(no-1)==0 and m.count(no+1)==0){
               m[no]=1;
           }
           if(m.count(no-1)==1 and m.count(no+1)==1){
               int len1=m[no-1];
               int len2=m[no+1];
               m[no]=1+len1+len2; //to make sure not processed again
               m[no-len1]=len1+len2+1;
               m[no+len2]=len1+len2+1;
           }
           if(m.count(no-1) and !m.count(no+1)){
               int len1=m[no-1];
               m[no-len1]=len1+1;
               m[no]=len1+1;
           }
           if(!m.count(no-1) and m.count(no+1)){
               int len1=m[no+1];
               m[no+len1]=len1+1;
               m[no]=len1+1;
           }
       }

can you tell what is happening ?with this testcase
20
6 6 2 3 1 4 1 5 6 2 8 7 4 2 1 3 4 5 9 6

why the code is giving 26 as the answer?
and how are the element reprocessed again herre?

Here printing values of 1-9 of map

6 : 0 0 0 0 0 1 0 0 0 cool
6 : 0 0 0 0 0 1 0 0 0 cool
2 : 0 1 0 0 0 1 0 0 0 cool
3 : 0 2 2 0 0 1 0 0 0 cool
1 : 3 2 3 0 0 1 0 0 0 cool
4 : 4 2 3 4 0 1 0 0 0 cool
1 : 3 2 3 4 0 1 0 0 0 here it decreased the value from 4 to 3
5 : 6 2 3 4 0 6 0 0 0 cool
6 : 6 2 3 4 0 1 0 0 0 here it decreased the value from 6 to 1
2 : 6 2 3 4 10 1 0 0 0 here it made value 10 can u see that length anywhere
8 : 6 2 3 4 10 1 0 1 0 …

okay got it @Kartikkhariwal1

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.