@@ -49,9 +49,7 @@ void longestSubSeq(int *arr, int size){
49
49
}
50
50
end = end + 1 ;
51
51
start = end - maxLength + 1 ;
52
- printf ("max length is %d\n" , maxLength );
53
- printf ("start value is %d\n" , start );
54
- printf ("end value is %d\n" , end );
52
+
55
53
//print the subsequence
56
54
for (i = start ;i <=end ;i ++ ){
57
55
printf ("%d\n" , arr [i ]);
@@ -67,5 +65,64 @@ int main(){
67
65
qsort (arr ,size ,sizeof (int ),cmpfunc );
68
66
69
67
longestSubSeq (arr , size );
68
+ return 0 ;
69
+ }
70
+
71
+ //================================================
72
+ //METHOD2:
73
+ #include <stdio.h>
74
+ #include <stdlib.h>
75
+
76
+ struct hash {
77
+ int data ;
78
+ };
79
+
80
+ void find (int * arr , int size ){
81
+ struct hash * hashArr = (struct hash * )malloc (sizeof (struct hash )* 100 ); //fixed size
82
+
83
+ printf ("%c\n" , hashArr [0 ].data );
84
+
85
+ int i ;
86
+ for (i = 0 ; i < size ;i ++ ){
87
+ hashArr [arr [i ]].data = 1 ;
88
+ }
89
+ int key , start , prev , maxLength = 0 ;
90
+ int startIndex = 0 , endIndex = 0 ;
91
+
92
+ for (i = 0 ;i < size ;i ++ ){
93
+ key = arr [i ];
94
+ prev = arr [i ];
95
+
96
+ if (hashArr [key ].data != 2 ){
97
+ while (hashArr [key ].data == 1 ){
98
+ hashArr [key ].data == 2 ;
99
+ key -- ;
100
+ }
101
+ start = key + 1 ;
102
+ key = prev + 1 ;
103
+ while (hashArr [key ].data == 1 ){
104
+ hashArr [key ].data = 2 ;
105
+ key ++ ;
106
+ }
107
+ prev = -- key ;
108
+ if (maxLength < prev - start + 1 ){
109
+ maxLength = prev - start + 1 ;
110
+ startIndex = start ;
111
+ endIndex = prev ;
112
+ }
113
+ }
114
+ }
115
+ int j ;
116
+ for (j = startIndex ; j <=endIndex ;j ++ ){
117
+ printf ("%d\n" , j );
118
+ }
119
+ }
120
+
121
+ int main (){
122
+ int arr [] = {10 ,4 ,3 ,11 ,13 ,5 ,6 ,12 ,7 };
123
+ int size = sizeof (arr )/sizeof (arr [0 ]);
124
+
125
+ find (arr ,size );
126
+
70
127
return 0 ;
71
128
}
0 commit comments