A building's elevator logs every floor it stops at during a single overnight run, in visiting order, as an array of floor numbers. A maintenance inspector wants to find the longest unbroken stretch of consecutive stops in that log forming a valid inspection window: the window must begin on an even-numbered floor, every floor number inside the window must be at most a given maintenance cap (floors above the cap are sealed off and could not have been safely visited), and, moving from each stop to the very next stop inside the window, the floor's parity (even or odd) must flip every single time.
Given the elevator's full stop log and the maintenance cap, find the length of the longest inspection window.
Print a single integer: the length of the longest inspection window, or 0 if no window of length at least 1 exists (i.e., no logged floor is both even and within the cap).
Example 1
Input
4 5 3 2 5 4
Expected
3
Explanation
Floors visited: 3, 2, 5, 4; cap = 5. Starting at index 0 is invalid (3 is odd). Starting at index 1 (floor 2, even and <=5): the window continues 2 -> 5 (odd, <=5, parity flips) -> 4 (even, <=5, parity flips again), giving a window of length 3, which is the longest possible.
Example 2
Input
2 2 1 2
Expected
1
Explanation
Floors visited: 1, 2; cap = 2. Index 0 (floor 1) is odd, so no window can start there. Index 1 (floor 2) is even and <=2, and the log ends immediately after, so the longest window has length 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 →