-
Notifications
You must be signed in to change notification settings - Fork 2
/
question14.c
55 lines (49 loc) · 1.18 KB
/
question14.c
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
/*
Find the longest decreasing sub sequence in a given array
METHOD:
It remains the same as that of increasing sub sequence (question13), here either we can reverse
the array and apply that or reverse the loop
Time complexity: O(n^2)
Space complexity: O(n)
*/
#include <stdio.h>
#include <stdlib.h>
int lonDecreasingSub(int *arr, int size){
int *sol = (int *)malloc(sizeof(int)*size);
int i,j;
int maxLength = 1;
sol[size-1] = 1;
for(i=size-2;i>=0;i--){
int max = 1;
for(j=i+1;j<size;j++){
int key = 1;
if(arr[i] > arr[j]){
key = key + sol[j];
// printf("key value for i = %d and j= %d is %d\n",i,j,key);
if(key > max){
// printf("updating max to %d\n", key);
max = key;
}
}
}
// printf("sol of i = %d is now %d\n", i, max);
// printf("==================================\n");
sol[i] = max;
if(maxLength < sol[i]){
maxLength = sol[i];
}
}
return maxLength;
}
int main(){
int *arr, size = 8;
arr = (int *)malloc(sizeof(int)*size);
int i;
for(i=0;i<size;i++){
printf("Enter the %d element\n", i);
scanf("%d",&arr[i]);
}
int len = lonDecreasingSub(arr,size);
printf("length of longest decreasing subsequence is %d\n", len);
return 0;
}