-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlongest_subsequence.cpp
More file actions
50 lines (47 loc) · 1.45 KB
/
longest_subsequence.cpp
File metadata and controls
50 lines (47 loc) · 1.45 KB
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
#include<iostream>
using namespace std;
#include <vector>
#include "solution.h"
#include<map>
vector<int> longestSubsequence(int *arr, int n){
// Write your code here
map<int,bool> m;
for(int i=0 ; i<n ; i++){ // ek map liya h usme sabke keys ki value true h;
m[arr[i]] = true;
}
int len=1,r_len=1;
int j = 1;
int start;
// cout<<m[15]<<endl;
for(int i=0 ; i<n ; i++){
j = 1;
len = 1;
if(m[arr[i] - j]==false){ //ye check kar rha h ki hum ekdum sequence k pehle number se count karen
while(m[arr[i] + j]){ //agar koi arr[i] se peeche ka element h map me toh loop mhi chalega i++ hoga tbb jb vo
len++; //pehle wala element aayega tbbhi shuru se count hoga
j++; // agar pehla element hi aarr[i] hoga toh uske pehla map me hi nhi hoga toh vo false hoga
} //tb vahan se count shuru.
if(r_len < len){
r_len = len;
start = arr[i]; //jis element se longest sequence nikal rha h usko pakad lo
}
}
}
vector<int> vect;
for(int i=0 ; i<r_len ; i++){
vect.push_back((start + i));
}
return vect;
}
int main() {
int n;
cin >> n;
int *input = new int[n];
for(int i = 0; i < n; i++){
cin >> input[i];
}
vector<int> output = longestSubsequence(input, n);
for(int i = 0; i < output.size(); i++) {
cout << output[i] << endl;
}
}