question–>
https://practice.geeksforgeeks.org/problems/longest-consecutive-subsequence2449/1#
code->
https://ide.codingblocks.com/s/381320
not passing all testcases
question–>
https://practice.geeksforgeeks.org/problems/longest-consecutive-subsequence2449/1#
code->
https://ide.codingblocks.com/s/381320
not passing all testcases
@sheikhhaji18 hey see this code. only diiference is i have used array instead of hashmap
int m = *max_element(arr,arr+n); //find the maximum element from the given array
int a[m+1]={0}; //after finding the max element make an array of size m+1 for hashing
for(int i=0;i<n;i++)
{
a[arr[i]]=1; //put 1 in place of 0
}
int result = 0; // this is the final subsequence
int ans = 0; // this is used for every index subsequence
for(int i=0;i<m+1;i++)
{
if(a[i]==1)
{
ans++; // if 1 exists just ++ our ans
}
else
{
ans = 0; //otherwise zero
}
result = max(ans,result); //in every step always check the maximum length of the subsequence
}
return result;
okay this is easy to understand, this is also O(n),
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.