C++ solution 6 lines


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

#2

Wow, best solution ever: short, clean and efficient. Congrats!


#3

can you explain the approach?? @ketan_chandak @raschidjfr