#K43527. Longest Trend in Stock Prices
Longest Trend in Stock Prices
Longest Trend in Stock Prices
You are given the closing prices of a stock over N days. A trend is defined as a contiguous subsequence of days in which, for every adjacent pair, the prices are either strictly increasing or strictly decreasing.
More formally, given a sequence \(p_1, p_2, \dots, p_N\), a contiguous segment \(p_i, p_{i+1}, \dots, p_j\) (with \(j \ge i\)) is called a trend if for every \(k\) such that \(i p_{k-1}\) for all such \(k\) (strictly increasing) or \(p_k < p_{k-1}\) for all such \(k\) (strictly decreasing). Note that when two consecutive prices are equal, the trend is broken, and the segment resets.
Your task is to compute the length of the longest trend.
inputFormat
The input is read from standard input (stdin). The first line contains an integer (N) denoting the number of days. The second line contains (N) space-separated integers representing the stock prices for each day.
outputFormat
Output a single integer to standard output (stdout) indicating the length of the longest contiguous trend.## sample
3
1 2 3
3