forked from v100901/hackoctoberfest2020__
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest consecutives subsequences.cpp
73 lines (65 loc) · 1.35 KB
/
longest consecutives subsequences.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
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
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 i;
vector<int>nums;
for(i=0;i<N;i++)
{
nums.push_back(arr[i]);
}
int count=0,mx=0;
unordered_set<int>s;
for(i=0;i<nums.size();i++)
{
s.insert(nums[i]);
}
vector<int>v;
for(auto x:s)
{
v.push_back(x);
}
sort(v.begin(),v.end());
count=1;
if(v.size()==0||v.size()==1)
{
return v.size();
}
count=1;
for(i=0;i<v.size()-1;i++)
{
if(v[i+1]==v[i]+1)
count++;
else
{
mx=max(mx,count);
count=1;
}
mx=max(mx,count);
}
return mx;
}
};
// { Driver Code Starts.
// Driver program
int main()
{
int t,n,i,a[100001];
cin>>t;
while(t--)
{
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
Solution obj;
cout<<obj.findLongestConseqSubseq(a, n)<<endl;
}
return 0;
} // } Driver Code Ends