A logistics engineer studies a conveyor belt lined end-to-end with speed sensors, numbered from the first checkpoint to the last, each recording the belt's instantaneous speed at that checkpoint. Two zones of the belt are called a matching pair if they have the same length, the first zone ends exactly at the checkpoint immediately before the second zone begins (no gap, no overlap — they are perfectly back-to-back), and within each zone the recorded speeds increase strictly from one checkpoint to the next.
Given the sequence of recorded speeds, find the greatest length k for which some matching pair of zones, each of length k, exists somewhere along the belt.
Note that a zone of length 1 trivially counts as strictly increasing (there is nothing to compare within it), so an answer of at least 1 is always achievable.
Line 1: an integer n, the number of sensors.
Line 2: n space-separated integers, the recorded speeds in checkpoint order.
A single integer: the greatest k for which a matching pair of length-k zones exists.
Example 1
Input
6 1 2 3 2 3 4
Expected
3
Explanation
Speeds are [1,2,3,2,3,4]. Take the zone [1,2,3] (checkpoints 0-2) and the adjacent zone [2,3,4] (checkpoints 3-5): both are strictly increasing, and they are back-to-back with no gap, giving k=3. No length-4 matching pair exists (that would need 8 checkpoints), so the answer is 3.
Example 2
Input
5 5 4 3 2 1
Expected
1
Explanation
Speeds are strictly decreasing throughout, so no two adjacent checkpoints form an increasing pair of length 2. The only matching pairs available have length 1 (e.g. zone [5] followed by zone [4]), so the answer is 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 →