A hiking app logs the elevation (in meters) recorded at each waypoint along a trail, in the order a hiker passes them. A streak is a contiguous run of waypoints along which every step either always climbs (each reading strictly greater than the one before) or always descends (each reading strictly less than the one before) — the direction must stay the same for the whole streak, but different streaks in the log may go in different directions. Given the elevation log, find the length of the longest streak.
n, the number of waypoints.n space-separated integers, the elevation readings in trail order.Example 1
Input
5 3 5 7 4 2
Expected
3
Explanation
Elevations rise strictly for the first three waypoints (3 < 5 < 7), a streak of length 3, then fall strictly for the last three waypoints starting at the peak (7 > 4 > 2), also length 3. No streak is longer, so the answer is 3.
Example 2
Input
3 5 5 5
Expected
1
Explanation
All three readings are equal, so no consecutive pair is strictly increasing or strictly decreasing; every streak is limited to a single waypoint, giving an answer of 1.
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 →