-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlongest_subsequence1.c
More file actions
56 lines (41 loc) · 1.08 KB
/
longest_subsequence1.c
File metadata and controls
56 lines (41 loc) · 1.08 KB
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
// Incomplete
#include <stdio.h>
#include <stdlib.h>
// int *lookup;
int longest_subsequence1( int n, int *arr ) {
int count[1000] = {0}, max = 1, i;
for( i = 0; i < n; i++ ) {
if( arr[i] == 0 )
continue;
count[arr[i]]++;
//printf("\ni = %d\tarr[i] = %d\tcount[arr[i]] = %d\tcount[arr[i]+1] = %d", i, arr[i], count[arr[i]], count[arr[i]+1] );
if( (count[arr[i]] + count[arr[i]-1]) > max ) {
max = (count[arr[i]] + count[arr[i]-1]);
if( max == 455)
printf("arr[%d]= %d",i, arr[i]);
}
if( (count[arr[i]] + count[arr[i]+1]) > max ) {
max = (count[arr[i]] + count[arr[i]+1]);
if( max == 455)
printf("arr[%d]= %d",i, arr[i]);
}
}
return max;
}
int main(int argc, char const *argv[])
{
int i, n, ans, t;
printf("\nEnter the number of test cases:");
scanf("%d",&t);
while(t-- ) {
printf("Enter the value of n:");
scanf("%d",&n);
int *arr = (int *)malloc( n * sizeof( int ));
printf("\nEnter the array:");
for( i = 0; i < n; i++ )
scanf("%d",&arr[i]);
ans = longest_subsequence1( n, arr );
printf("\nAns = %d",ans);
}
return 0;
}