#include <bits/stdc++.h>
using namespace std;
bool linearSearch(vector<int>&a, int num) {
int n = a.size(); //size of array
for (int i = 0; i < n; i++) {
if (a[i] == num)
return true;
}
return false;
}
int longestSuccessiveElements(vector<int>&a) {
int n = a.size(); //size of array
int longest = 1;
//pick a element and search for its
//consecutive numbers:
for (int i = 0; i < n; i++) {
int x = a[i];
int cnt = 1;
//search for consecutive numbers
//using linear search:
while (linearSearch(a, x + 1) == true) {
x += 1;
cnt += 1;
}
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+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpib29sIGxpbmVhclNlYXJjaCh2ZWN0b3I8aW50PiZhLCBpbnQgbnVtKSB7CiAgICBpbnQgbiA9IGEuc2l6ZSgpOyAvL3NpemUgb2YgYXJyYXkKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgaWYgKGFbaV0gPT0gbnVtKQogICAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KICAgIHJldHVybiBmYWxzZTsKfQppbnQgbG9uZ2VzdFN1Y2Nlc3NpdmVFbGVtZW50cyh2ZWN0b3I8aW50PiZhKSB7CiAgICBpbnQgbiA9IGEuc2l6ZSgpOyAvL3NpemUgb2YgYXJyYXkKICAgIGludCBsb25nZXN0ID0gMTsKICAgIC8vcGljayBhIGVsZW1lbnQgYW5kIHNlYXJjaCBmb3IgaXRzCiAgICAvL2NvbnNlY3V0aXZlIG51bWJlcnM6CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGludCB4ID0gYVtpXTsKICAgICAgICBpbnQgY250ID0gMTsKICAgICAgICAvL3NlYXJjaCBmb3IgY29uc2VjdXRpdmUgbnVtYmVycwogICAgICAgIC8vd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