Last active
August 3, 2019 10:28
-
-
Save vishalkanaujia/8ccef253b12a1d726ae5b603840ec37c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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