-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongconseqsubseq.cpp
54 lines (51 loc) · 1.2 KB
/
longconseqsubseq.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
#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i <= b; i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
//string s;
//getline(cin, s);
int findLongestConseqSubseq(int arr[], int N)
{
//Your code here
map<int, int> m;
int count = 0, prev = INT_MAX, max_count = 0;
for(int i = 0; i < N; i++){
if(arr[i] < prev) prev = arr[i];
}
prev = prev -1;
for(int i = 0; i < N; i++){
if(m.count(arr[i])){
m[arr[i]]++;
}
else m[arr[i]] = 1;
}
for(auto i: m){
if(i.second){
if(i.first == (prev+1)) {count++; prev = i.first; max_count = max(max_count, count);}
else{
max_count = max(max_count, count);
//cout<<max_count;
count = 0;
}
}
}
return max_count;
}
int main(){
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int n = 0;
int arr[] = {8, 8, 8, 9, 3, 4, 4};
n = findLongestConseqSubseq(arr, 7);
cout<<n;
return 0;
}
//g++ -std=c++11 -O2 -Wall test.cpp -o test