Skip to content

Commit 97832a0

Browse files
Create Longest_non-decreasing_subsequence.c
1 parent 72260d6 commit 97832a0

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Problem statement:
2+
3+
// Find the length of the Longest Non-decreasing Subsequence in a given Sequence.
4+
5+
// Eg:
6+
7+
// Input:9
8+
9+
// Sequence:[-1,3,4,5,2,2,2,2,3]
10+
11+
// the subsequence is [-1,2,2,2,2,3]
12+
13+
// Output:6
14+
15+
16+
17+
// NOTE: This code is not based on Dynamic programming, it's just normal execution.
18+
#include<stdio.h>
19+
int find(int n,int a[])
20+
{
21+
int ans=0;
22+
{
23+
for(int i=0;i<n;i++)
24+
{
25+
if(ans>n-i)
26+
return ans;
27+
else
28+
{
29+
int l=i,r=n-1,t=1;
30+
while(l<r)
31+
{
32+
33+
if(a[l]<=a[r] && (l==i || a[l]>=a[l-1]) )
34+
{
35+
if(a[r]>=a[r-1])
36+
r--;
37+
else
38+
l++;
39+
t++;
40+
}
41+
else if(a[l+1]>=a[l])
42+
{
43+
l++;
44+
r++;
45+
}
46+
else
47+
break;
48+
}
49+
if(ans<t)
50+
ans=t;
51+
}
52+
}
53+
return ans;
54+
}
55+
}
56+
int main()
57+
{
58+
int n;
59+
scanf("%d",&n);
60+
int a[n];
61+
for(int i=0;i<n;i++)
62+
scanf("%d",&a[i]);
63+
printf("%d",find(n,a));
64+
}

0 commit comments

Comments
 (0)