Skip to content

Commit 5da0677

Browse files
committed
new question added
1 parent 679b876 commit 5da0677

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ linear array, array of length 1 less than total can be taken and a pattern can b
376376
- [Given a binary matrix, find the largest square sub matrix with all 1's](/dynamic-programming/question11.c)
377377
- [Find kth ugly number](/dynamic-programming/question12.c)
378378
- [Find the longest increasing subsequence](/dynamic-programming/question13.c)
379+
- [Find the longest decreasing subsequence](/dynamic-programming/question14.c)
380+
379381

380382
## Some important concepts to solve algos better
381383

dynamic-programming/a.exe

81 Bytes
Binary file not shown.

dynamic-programming/question14.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Find the longest decreasing sub sequence in a given array
3+
4+
METHOD:
5+
It remains the same as that of increasing sub sequence (question13), here either we can reverse
6+
the array and apply that or reverse the loop
7+
8+
Time complexity: O(n^2)
9+
Space complexity: O(n)
10+
*/
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
14+
int lonDecreasingSub(int *arr, int size){
15+
int *sol = (int *)malloc(sizeof(int)*size);
16+
int i,j;
17+
int maxLength = 1;
18+
sol[size-1] = 1;
19+
for(i=size-2;i>=0;i--){
20+
int max = 1;
21+
for(j=i+1;j<size;j++){
22+
int key = 1;
23+
if(arr[i] > arr[j]){
24+
key = key + sol[j];
25+
// printf("key value for i = %d and j= %d is %d\n",i,j,key);
26+
if(key > max){
27+
// printf("updating max to %d\n", key);
28+
max = key;
29+
}
30+
}
31+
}
32+
// printf("sol of i = %d is now %d\n", i, max);
33+
// printf("==================================\n");
34+
sol[i] = max;
35+
if(maxLength < sol[i]){
36+
maxLength = sol[i];
37+
}
38+
}
39+
return maxLength;
40+
}
41+
42+
int main(){
43+
int *arr, size = 8;
44+
arr = (int *)malloc(sizeof(int)*size);
45+
46+
int i;
47+
for(i=0;i<size;i++){
48+
printf("Enter the %d element\n", i);
49+
scanf("%d",&arr[i]);
50+
}
51+
52+
int len = lonDecreasingSub(arr,size);
53+
printf("length of longest decreasing subsequence is %d\n", len);
54+
return 0;
55+
}

nextquestions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ TODO:
8282
- DP question6 to be done
8383
- DP question7 to be done
8484
- DP question11 (method1 naive) to be done
85-
- DP question13 (printing the subsequence to be done)
85+
- DP question13 and 14(printing the subsequence to be done)

0 commit comments

Comments
 (0)