Obstacle Course
InterviewTime limit1sMemory limit2048 MB
Given heights on odd-numbered segments with free even-numbered segments, find the longest run of consecutive segments where Adrian can step up or down by exactly 1 each time.
- Level
Medium4 of 10
- Topics
- Array, Implementation
- Solved
- No attempts yet
Problem
Morgan is a businessman and parkour enthusiast who owns an obstacle course of road segments, numbered from to . Segment is adjacent to segment , segment is adjacent to segment , and so on, up to segment adjacent to segment . Each odd-numbered segment has a positive integer height , and each even-numbered segment has height . On Morgan's course, anyone can start from any segment, move in one direction, and stop at any segment they can reach.
Adrian is a beginner. He keeps going while the height difference between the current segment and the next one is exactly . If the difference is more than , he cannot climb up or jump down, so he stops. If the next segment has the same height as the current one, he loses interest and stops. He also stops when he reaches segment or segment , the end of the course. Adrian's happiness is the number of segments he passes before he stops. He starts from whichever segment gives him the largest happiness.
Morgan wants Adrian to enjoy the course, so he changes the heights of some even-numbered segments (possibly none) to maximize Adrian's largest happiness. Find the largest happiness Adrian can have after the changes.
For example, let . If is changed to , Adrian can go from segment 1 to 4, or from segment 6 to 9, and his happiness is 4, which is the largest after the change. Other changes also reach 4, such as , but no change can make the value larger than 4.
Input
The first line contains an integer (). The second line contains integers . if is even, and if is odd.
Output
Print one integer on a single line: the largest possible happiness of Adrian on Morgan's new course.