-
Notifications
You must be signed in to change notification settings - Fork 40
/
longest_bitonic_subsequence.c
97 lines (89 loc) · 2.36 KB
/
longest_bitonic_subsequence.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Date: 2018-10-20
*
* Description:
* Given an array arr[0 … n-1] containing integers, a subsequence of arr[] is
* called Bitonic if it is first increasing, then decreasing.
* Write a function that takes an array as argument and returns the length of
* the longest bitonic subsequence.
* A sequence, sorted in increasing order is considered Bitonic with the
* decreasing part as empty. Similarly, decreasing order sequence is considered
* Bitonic with the increasing part as empty.
*
* Examples:
* Input arr[] = {1, 11, 2, 10, 4, 5, 2, 1};
* Output: 6 (A Longest Bitonic Subsequence of length 6 is 1, 2, 10, 4, 2, 1)
*
* Approach:
* Take 2 auxiliary arrays inc[] and dec[], such that inc[i] and dec[i] keeps
* track of number of elements in increasing order from 0 to a[i] and decreasing
* from a[i] to a[n-1] respectively.
* Max bitonic length would be max of inc[i] + dec[i] - 1.
* -1 is done as a[i] would be part of both inc[i] and dec[i]
*
* Complexity:
* Time - O(N)
* Space - O(N)
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0;
int n = 0;
int *a = NULL;
int *inc = NULL, *dec = NULL;
int max = 1;
printf("Enter number of elements: ");
scanf("%d", &n);
a = (int *)malloc(sizeof(int)*n);
inc = (int *)malloc(sizeof(int)*n);
dec = (int *)malloc(sizeof(int)*n);
for (i = 0; i < n; i++) {
printf("Enter element[%d]: ", i);
scanf("%d", &a[i]);
inc[i] = 1;
dec[i] = 1;
}
for (i = 1; i < n; i++) {
inc[i] = a[i] > a[i - 1] ? inc[i - 1] + 1 : 1;
printf("%d ", inc[i]);
}
printf ("\n");
for (i = n - 2; i >= 0; i--) {
dec[i] = a[i] > a[i + 1] ? dec[i + 1] + 1 : 1;
printf("%d ", dec[i]);
}
for (i = 0; i < n; i++) {
if (max < inc[i] + dec[i])
max = inc[i] + dec[i];
}
printf("\nMax biotic length is: %d\n", max - 1);
return 0;
}
/*
* Output:
* ---------------------
* Enter number of elements: 5
* Enter element[0]: 2
* Enter element[1]: 3
* Enter element[2]: 4
* Enter element[3]: 1
* Enter element[4]: 0
* 2 3 1 1
* 2 3 1 1
* Max biotic length is: 5
*
* Enter number of elements: 9
* Enter element[0]: 2
* Enter element[1]: 3
* Enter element[2]: 5
* Enter element[3]: 1
* Enter element[4]: 0
* Enter element[5]: 9
* Enter element[6]: 4
* Enter element[7]: 5
* Enter element[8]: 7
* 2 3 1 1 2 1 2 3
* 1 1 2 1 2 3 1 1
* Max biotic length is: 5
*/