-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_seq.cpp
73 lines (62 loc) · 1.6 KB
/
longest_seq.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<iostream>
#include<vector>
using namespace std ;
//This function is onlyt to get the ceile index
int GetIndex(int arr[], std::vector<int> &temp , int l , int r , int key){
while(r - l > 1 ){
int m = l + (r - l ) /2 ;
if(arr[temp[m]] >= key)
r = m ;
else
l = m ;
}
return r ;
}
// This is main functiuonm to get the longest increasing sequence
int getLongestIncreasingSequence(int v[] , int n ){
for(int i = 0 ; i < n; i++)
cout<<v[i]<<" " ;
cout<<endl;
std::vector<int> temp(n , 0 );
std::vector<int> res(n , -1 );
int len = 1 ; //default length
for(int i = 1 ; i < n; i++){
if(v[i] < v[temp[0]]){
temp[0] = i ;
}
else if ( v[i] > v[temp[len - 1]]){
res[i] = temp[len - 1] ;
temp[len++] = i ;
}
else{
//Get the ceilIndex
int pos = GetIndex(v , temp , -1 , len-1 , v[i]);
res[i] = temp[pos - 1] ;
temp[pos] = i ;
}
}
//Printing the rtemporary array
cout<<"print temp"<<endl ;
for(int i = 0 ; i < n; i++)
cout<<temp[i]<<" " ;
cout<<endl;
//Printing the result array
cout<<"print res"<<endl ;
for(int i = 0 ; i < n; i++)
cout<<res[i]<<" ";
cout<<endl ;
//Printing the increasing order sequence
for(int i = temp[len-1] ; i >0 ; i= res[i])
cout<<v[i]<<" ";
cout<<endl ;
return len ;
}
int main(){
int arr[] = {2, 5, 3, 7, 11, 8, 10, 13, 6};
// vector<int> v (arr, arr + sizeof(arr) / sizeof(arr[0]) );
// std::vector<int> v(2, 5, 3, 7, 11, 8, 10, 13, 6);
// int n = sizeof(arr) / sizeof(arr[0]) ;
int n = sizeof(arr) / sizeof(arr[0]);
cout<< getLongestIncreasingSequence(arr,n)<<endl;
return 0 ;
}