TopCoder problem "MonotoneSequence" used in SRM 388 (Division II Level One)



Problem Statement

    

A strictly increasing sequence is a sequence of numbers where each number is strictly greater than the previous one. A strictly decreasing sequence is a sequence where each number is strictly less than the previous one. A strictly monotone sequence is a sequence that is either strictly increasing or strictly decreasing. For example, 1, 5, 6, 10 and 9, 8, 7, 1, are strictly monotone sequences, while 1, 5, 2, 6 and 1, 2, 2, 3 are not.

Given a sequence seq, determine the length of the longest contiguous subsequence that is strictly monotone (see examples for clarifications).

 

Definition

    
Class:MonotoneSequence
Method:longestMonotoneSequence
Parameters:int[]
Returns:int
Method signature:int longestMonotoneSequence(int[] seq)
(be sure your method is public)
    
 

Constraints

-seq will contain between 1 and 50 elements, inclusive.
-Each element of seq will be between 1 and 100, inclusive.
 

Examples

0)
    
{1, 7, 7, 8, 3, 6, 7, 2}
Returns: 3
The longest contiguous monotone subsequence is 3, 6, 7. The sequence 1, 3, 6, 7 is not valid because 1 and 3 are not adjacent, and 1, 7, 7, 8 is not valid because it is not strictly increasing.
1)
    
{1, 1, 1, 1, 1}
Returns: 1
A sequence of one element is valid.
2)
    
{10, 20, 30, 25, 20, 19, 20, 18, 23}
Returns: 4
3)
    
{3, 2, 1, 4}
Returns: 3

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8564

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=11122&pm=8564

Writer:

bmerry

Testers:

PabloGilberto , Olexiy , marek.cygan , ivan_metelsky

Problem categories:

Simple Search, Iteration