can you please explain me its time complexity…
Longest subsequence implementation
Hey @gulatigarvita08
the approach we are following is -
a) if current-1 element is not there in set then start iterating from current (temp variable -> x) till s.find(x) !=s.end() and increment x by one.update max accordingly
b) if current -1 is not equal to s.end() then it means current element is already considered in previous counting so we ignore it (I.e continue to next number)
so we can say every element is considered only twice i.e 2*n in worst
that’s why time complexity is O(n)
if say i create a set from array…then do the following:
sets;
int cnt=0;
int len=0;
for(int i=0;i<n;i++)
{ s.insert(arr[i]); }
for(auto p:s)
{
if(s.find§!=s.end()
{
++cnt;
}
else
{
len=max(len,cnt);
cnt=0;
}
}
i guess i will also take o(n) time…can’t we use it??
I am not able to understand please elaborate and also share your code on IDE instead.
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.