Skip to content

Commit fd95ff5

Browse files
committed
new question added
1 parent 8d8be98 commit fd95ff5

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

dynamic-programming/a.exe

-34 Bytes
Binary file not shown.

dynamic-programming/question10.c

+60-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ void longestSubSeq(int *arr, int size){
4949
}
5050
end = end + 1;
5151
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+
5553
//print the subsequence
5654
for(i=start;i<=end;i++){
5755
printf("%d\n", arr[i]);
@@ -67,5 +65,64 @@ int main(){
6765
qsort(arr,size,sizeof(int),cmpfunc);
6866

6967
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+
70127
return 0;
71128
}

0 commit comments

Comments
 (0)