A markets analyst logs the price of a single stock at n evenly spaced ticks during one trading session. A session is called a peak-valley-recovery session if the logged prices can be split into three consecutive, non-empty stretches: an opening rally that strictly climbs up to some tick p (the session's peak), a sell-off that strictly falls from tick p down to some later tick q (the session's trough), and a closing rally that strictly climbs from tick q all the way to the final tick.
Formally, the session qualifies if there exist indices 0 <= p < q <= n - 1 such that:
prices[0] < prices[1] < ... < prices[p]prices[p] > prices[p+1] > ... > prices[q]prices[q] < prices[q+1] < ... < prices[n-1]A stretch that covers only a single index is automatically valid, since it contains no adjacent pair to compare. Given the logged prices, decide whether the session qualifies.
n — the number of logged ticks (3 <= n <= 100000).n space-separated integers prices[0], prices[1], ..., prices[n-1] (1 <= prices[i] <= 10^9).Print true if the session is a peak-valley-recovery session, or false otherwise.
3 <= n <= 1000001 <= prices[i] <= 10^9Example 1
Input
7 1 3 5 4 2 3 6
Expected
true
Explanation
The prices climb 1 -> 3 -> 5 (peak at index 2), fall 5 -> 4 -> 2 (trough at index 4), then climb 2 -> 3 -> 6 through the close. Taking p=2 and q=4 satisfies all three strict-monotonic conditions, so the session qualifies: true.
Example 2
Input
4 2 4 6 8
Expected
false
Explanation
The prices climb for the entire session with no sell-off at all, so there is no index q > p where the prices ever strictly decrease. No valid (p, q) pair exists, so the session does not qualify: false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →