[2, 9, 4, 6, 1] is a seesaw (differences +7, −5, +2, −5), while [2, 5, 8, 1] is not: its first two differences are both positive.
You get an array values. You may cross out any of its elements, keeping the rest in their original order. Return the greatest length of a seesaw you can get this way.
Can you do it in O(n)?values=[3, 8, 8, 5, 6, 2, 2, 9] → 6
For example 3, 8, 5, 6, 2, 9: the differences +5, −3, +1, −4, +7 alternate in sign, length 6. The repeated 8 and 2 have to go, because a difference of 0 is neither a rise nor a fall.
values=[10, 20, 30, 40] → 2
The sequence only goes up, so nothing longer than two elements works, for example 10, 40.
values=[7, 7, 7] → 1
values=[3, 8, 8, 5, 6, 2, 2, 9]
6
Run checks the first 3 tests in your browser. Submit checks all 136 tests on the server, hidden ones included.