# My C++ solution using a little bit different point of view in the (hash)map

• ``````class Solution {
public:
int longestConsecutive(vector<int>& nums) {
map<int, pair<int,int>> t;
int longest = 0;
for (auto d:nums) {
int l = d-1;
int r = d+1;
if (t.find(d) != t.end() && t[d].first != -1 && t[d].second != -1) {
int lw = t[d].first;
int rw = t[d].second;
int span = lw + 1 + rw;
longest = max(span, longest);
if (t.find(l-lw) == t.end())
t[l-lw] = make_pair(0,0);
t[l-lw].second = span;
if (t.find(r+rw) == t.end())
t[r+rw] = make_pair(0,0);
t[r+rw].first = span;
t[d].first = -1;
t[d].second = -1;
}
else if (t.find(d) == t.end()) {
longest = max(longest, 1);
if (t.find(l) != t.end() && t[l].second != -1) {
t[l].second = max(t[l].second, 1);
}
else {
if (t.find(l) == t.end())
t[l] = make_pair(0,1);
}
if (t.find(r) != t.end() && t[r].first != -1) {
t[r].first = max(t[r].first, 1);
}
else {
if (t.find(r) == t.end())
t[r] = make_pair(1,0);
}
}
}
return longest;
}
};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.