-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path19. Longest consecutive subsequence.cpp
146 lines (131 loc) · 3.26 KB
/
19. Longest consecutive subsequence.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Problem link - https://bit.ly/3TMU9ka
// Approach - 1
class Solution{
public:
// arr[] : the input array
// N : size of the array arr[]
//Function to return length of longest subsequence of consecutive integers.
int findLongestConseqSubseq(int arr[], int N)
{
//Your code here
sort(arr, arr+N);
int ans=1, longest=1, last=arr[0];
for(int i=1;i<N;i++){
if(arr[i]==last)
continue;
if(arr[i]==last+1){
longest++;
last++;
}
else ans=max(ans, longest), longest=1, last=arr[i];
}
ans=max(ans, longest);
return ans;
}
};
/*
Time complexity: O(nlogn)
Space complexity: O(1)
*/
// Approach - 2: Using a set
class Solution{
public:
// arr[] : the input array
// N : size of the array arr[]
int findLongestConseqSubseq(int arr[], int N)
{
//Your code here
unordered_set<int>s(arr,arr+N);
int ans=0;
for(auto x:s){
// if x-1 is not there, start from the smallest number in the series
// and check for consecutive numbers using set
if(s.count(x-1)==0){
int count=1;
while(s.count(x+count)!=0)count++;
ans=max(ans,count);
}
}
return ans;
}
};
/*
Time complexity: O(nlogn)
Space complexity: O(1)
*/
class DSU {
vector<int> par, rank;
public:
DSU(int n){
par.resize(n);
rank.resize(n);
for(int i=0;i<n;i++)
par[i]=i, rank[i]=1;
}
int find(int a){
if(a==par[a])
return a;
return par[a]=find(par[a]);
}
void union_(int a, int b){
a=find(a), b=find(b);
if(a==b)
return;
if(rank[b]>=rank[a]){
// b dominates
rank[b]+=rank[a];
par[a]=b;
}
else{
rank[a]+=rank[b];
par[b]=a;
}
}
int res(int n){
int ans=0;
for(int i=0;i<n;i++){
ans=max(ans, rank[i]);
}
return ans;
}
};
class Solution{
public:
// arr[] : the input array
// N : size of the array arr[]
//Function to return length of longest subsequence of consecutive integers.
int findLongestConseqSubseq(int arr[], int N)
{
//Your code here
int n=N;
// we union the indices
DSU dsu(n);
unordered_map<int, int> mp;
for(int i=0;i<n;i++){
// to find consecutive elements faster
if(mp.find(arr[i])==mp.end())
mp[arr[i]]=i;
}
unordered_set<int> vis;
for(int i=0;i<n;i++){
if(vis.find(arr[i])!=vis.end()){
continue;
}
if(mp.find(arr[i]-1)!=mp.end()){
dsu.union_(mp[arr[i]-1], i);
}
if(mp.find(arr[i]+1)!=mp.end()){
dsu.union_(mp[arr[i]+1], i);
}
vis.insert(arr[i]);
}
return dsu.res(n);
}
};
/*
Time Complexity: O(n)
Space Complexity: O(n)
*/
// Code by Shumbul Arifa - https://linktr.ee/shumbul
// Follow 21 days DSA Challenge - www.shumbularifa.com
// Video solutions available on my YouTube