To find out max sorted contiguous subsequence of an array

void maxSorted(int a[], int n) 
{
int s[n];
int i;

s[n-1] = 0;

for(i = n - 2; i >= 0; --i)
{
if(a[i] < a[i +1])
s[i] = s[i+1] + 1;
}

int max = n-1;

for( i = n -2; i >= 0; --i)
{
if(s[i] > s[max])
max = i;

}

for(i = max; i <= max + s[max]; ++i)
printf("%d ", a[i]);

printf("\n");
}

int main()
{
int a[] = {2, 3, 1, 2, 3, 4, 5, 2, 1, 3};
maxSorted(a, sizeof(a)/sizeof(int));
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s