-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestConsecutiveSubsequence.cpp
More file actions
45 lines (40 loc) · 973 Bytes
/
longestConsecutiveSubsequence.cpp
File metadata and controls
45 lines (40 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
// longest consecutive subsequence
void naive(vector<int> a){
sort(a.begin(), a.end());
int count = 1;
int ans = 1;
for(int i = 0; i < a.size(); i++){
if(i > 0 && a[i] == a[i-1]+1){
count++;
}else{
ans = max(count, ans);
count = 1;
}
}
ans = max(count, ans);
cout << ans << endl;
}
void optimal(vector<int> a){
int ans = 0;
int curr = 1;
unordered_set<int> s(a.begin(), a.end());
for(int i = 0; i < a.size(); i++){
if(s.find(a[i]-1) != s.end()){
curr = 1;
while(s.find(a[i]+curr) != s.end()){
curr++;
}
ans = max(ans, curr);
}
}
cout << ans << endl;
}
int main(){
vector<int> a = {8, 20, 7, 30}; // 2;
// vector<int> a = {20, 30, 40}; // 1;
// vector<int> a = {1, 9, 3, 4, 2, 20}; // 4;
optimal(a);
return 0;
}