Easy C++ solution {O(n)}

programming
Tags: #<Tag:0x00007f1820babc08>

#1

int Solution::longestConsecutive(const vector &A)
{
int maxx=1;
unordered_map<int, int>M;
for(int i : A)
{
if(M[i]==0)
{
M[i] = M[i] + M[i-1] + M[i+1] + 1;
maxx = max(maxx,M[i]);
M[i+M[i+1]] = M[i];
M[i-M[i-1]] = M[i];
}
}
return maxx;
}