Skip to content

Commit 8d8be98

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

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ i+given number of times to see if thats true or not.
359359
- [Travelling salesman problem](/dynamic-programming/question6.c)
360360
- [All pair shortest path algorithm](/dynamic-programming/question7.c)
361361
- [Find the maximum sum sub array](/dynamic-programming/question8.c)
362-
- [Find the maximum sum sub array](/dynamic-programming/question9.c)
362+
- [Find the max sum increasing sub sequence](/dynamic-programming/question9.c)
363+
- [Find the longest subsequence in an array such that elements are consecutive](/dynamic-programming/question10.c)
363364

364365
## Some important concepts to solve algos better
365366

dynamic-programming/a.exe

65 Bytes
Binary file not shown.

dynamic-programming/question10.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Find the longest subsequence in an array such that elements are consecutive
3+
4+
Eg picking 10 from 2nd index 11 from 10th index and 12 from 15th index
5+
but 10 11 12 are consecutive as natural numbers
6+
7+
8+
METHOD1:
9+
Sort the array, then traverse it once to find the longest subsequence which contains consecutive
10+
elements
11+
12+
Time complexity: O(nlogn)
13+
Space complexity: O(1)
14+
15+
METHOD2:
16+
Maintain a hash table with elements that are present marked as 1. Now scan the array again and
17+
for the first number see all the consecutive numbers lesser than that to find the starting value
18+
of the sequence. Once found, see numbers greater than that, that are consecutive to find out
19+
end of sub sequence. Once numbers are visited or made part of a subsequence, mark them as processed
20+
by maintaining a flag. Repeat this for all numbers and output a longest subsequence.
21+
22+
Time complexity: O(n)
23+
//as in worst case all elements will be examined twice in the second traversal.
24+
25+
Space complexity: O(n)
26+
*/
27+
//METHOD1
28+
#include <stdio.h>
29+
#include <stdlib.h>
30+
31+
int cmpfunc(const void *a, const void *b){
32+
return (*(int *)a - *(int *)b);
33+
}
34+
35+
void longestSubSeq(int *arr, int size){
36+
int i;
37+
int start,end=0,length=1;
38+
int maxLength = 0;
39+
for(i=0;i<size-1;i++){
40+
if(arr[i+1] - arr[i] == 1){
41+
length++;
42+
if(length > maxLength){
43+
maxLength = length;
44+
end = i;
45+
}
46+
}else{
47+
length = 1;
48+
}
49+
}
50+
end = end + 1;
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);
55+
//print the subsequence
56+
for(i=start;i<=end;i++){
57+
printf("%d\n", arr[i]);
58+
}
59+
60+
}
61+
62+
int main(){
63+
int arr[] = {15,14,10,4,3,11,13,5,6,12,7};
64+
65+
int size = sizeof(arr)/sizeof(arr[0]);
66+
67+
qsort(arr,size,sizeof(int),cmpfunc);
68+
69+
longestSubSeq(arr, size);
70+
return 0;
71+
}

0 commit comments

Comments
 (0)