heights. Call a contiguous stretch of the array a **seesaw** if it has at least 2 elements and looks like a, a+1, a, a+1, …: the value goes up by exactly 1, then down by exactly 1, then up by 1 again, and so on, taking turns.
Return the length of the longest seesaw in the array, or -1 if there is none.heights=[7, 5, 6, 5, 6, 5, 9] → 5
The stretch [5, 6, 5, 6, 5] alternates between 5 and 6 and starts with the smaller one, so its length is 5.
heights=[3, 2, 3, 4] → 2
[2, 3] works, but [3, 2, 3] does not, since the stretch has to start by going up. [2, 3, 4] fails too: a step up must be followed by a step down. The answer is 2.
heights=[4, 4, 1] → -1
No pair of neighbours goes up by exactly 1, so the answer is -1.
heights=[7, 5, 6, 5, 6, 5, 9]
5
Run checks the first 3 tests in your browser. Submit checks all 159 tests on the server, hidden ones included.