#include <bits/stdc++.h>
using namespace std;
int longestSuccessiveElements(vector<int>&a) {
int n = a.size();
if (n == 0) return 0;
//sort the array:
sort(a.begin(), a.end());
int lastSmaller = INT_MIN;
int cnt = 0;
int longest = 1;
//find longest sequence:
for (int i = 0; i < n; i++) {
if (a[i] - 1 == lastSmaller) {
//a[i] is the next element of the
//current sequence.
cnt += 1;
lastSmaller = a[i];
}
else if (a[i] != lastSmaller) {
cnt = 1;
lastSmaller = a[i];
}
longest = max(longest, cnt);
}
return longest;
}
int main()
{
vector<int> a = {100, 200, 1, 2, 3, 4};
int ans = longestSuccessiveElements(a);
cout << "The longest consecutive sequence is " << ans << "\n";
return 0;
}
CgoKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbG9uZ2VzdFN1Y2Nlc3NpdmVFbGVtZW50cyh2ZWN0b3I8aW50PiZhKSB7CiAgICBpbnQgbiA9IGEuc2l6ZSgpOwogICAgaWYgKG4gPT0gMCkgcmV0dXJuIDA7CgogICAgLy9zb3J0IHRoZSBhcnJheToKICAgIHNvcnQoYS5iZWdpbigpLCBhLmVuZCgpKTsKICAgIGludCBsYXN0U21hbGxlciA9IElOVF9NSU47CiAgICBpbnQgY250ID0gMDsKICAgIGludCBsb25nZXN0ID0gMTsKCiAgICAvL2ZpbmQgbG9uZ2VzdCBzZXF1ZW5jZToKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgaWYgKGFbaV0gLSAxID09IGxhc3RTbWFsbGVyKSB7CiAgICAgICAgICAgIC8vYVtpXSBpcyB0aGUgbmV4dCBlbGVtZW50IG9mIHRoZQogICAgICAgICAgICAvL2N1cnJlbnQgc2VxdWVuY2UuCiAgICAgICAgICAgIGNudCArPSAxOwogICAgICAgICAgICBsYXN0U21hbGxlciA9IGFbaV07CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYgKGFbaV0gIT0gbGFzdFNtYWxsZXI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