Array ( [0] => [1] => questions [2] => Patterns [3] => Longest-Progressive-Sequence ) Codevita Previous Questions | Longest Progressive Sequence | THE INQUISITIVE





Longest Progressive Sequence

LEVEL:Beginner

Description

Given a sequence, the objective is to find the longest progressive sequence arranged in ascending order
A sequence is said to be progressive if it doesn't decrease at any point in time.
For example 1 1 2 2 is a progressive sequence but 1 2 1 is not a progressive sequence. Let S be the sequence and be represented by L spaced integers Ki, now your task is to find out the first longest progressive sequence present in the given sequence (S).

Input Format

First line will contain T, the length of the sequence and next line will contain T spaced integers Ki (where i = 0,1,L).
Line 1 T,where T is the length of the sequence
Line 2 Ki,where Ki is integer in sequence separated by space

Output Format

print the longest progressive sequence present in the given sequence


Example 1:

Input
4
1 1 2 1
Output
1 1 2
Example 2:

Input
9
1 2 3 3 2 4 8 9 10
Output
2 4 8 9 10
Example 3:

Input
7
1 3 5 7 2 6 8
Output
1 3 5 7

oops

Login to see Discussion




Approach

Take a new array and keep inserting values into that array.
Check whether the size of the array is greater than temp array and new value is greater than last element of the array.
If false, then store the array into temp and clear this array.
Repeat this process till end of the array.
Finally check size of temp and array and return the one with maximum size.


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution