Skip to content

Instantly share code, notes, and snippets.

@vishalkanaujia
Last active August 3, 2019 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishalkanaujia/8ccef253b12a1d726ae5b603840ec37c to your computer and use it in GitHub Desktop.
Save vishalkanaujia/8ccef253b12a1d726ae5b603840ec37c to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
int findSequence(vector<int> v) {
unordered_set<int> hashSet;
for(auto i = v.begin(); i != v.end(); i++) {
hashSet.insert(*i);
}
int length = 0;
int max = 0;
for(auto i = v.begin(); i != v.end(); i++) {
if (hashSet.find(*i - 1) != hashSet.end()) {
continue;
}
int start = *i;
while (hashSet.find(start++) != hashSet.end()) {
length++;
}
if (length > max) {
max = length;
length = 0;
}
}
return max;
}
int main() {
vector<int> v = {1,4,5,6,3};
cout << findSequence(v) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment