nums and an integer cap. Find the longest contiguous stretch of the array such that:
- its first element is even;
- neighbouring elements alternate in parity: even, odd, even, …;
- no element is greater than cap.
Return the length of that stretch, or 0 if no stretch qualifies.nums=[7, 4, 9, 2, 5, 8], cap=8 → 3
[4, 9, 2, 5, 8] starts even and alternates parity, but 9 exceeds 8. The best choice is [2, 5, 8], length 3.
nums=[3, 5, 7], cap=10 → 0
There are no even numbers, so no stretch can start: the answer is 0.
nums=[6, 6, 1, 4], cap=6 → 3
The two 6s share a parity, so the best stretch is [6, 1, 4].
nums=[7, 4, 9, 2, 5, 8], cap=8
3
Run checks the first 3 tests in your browser. Submit checks all 124 tests on the server, hidden ones included.