A warehouse logs its conveyor belt's speed once per minute for a full shift, producing an array of n readings. A maintenance engineer is looking for the longest possible "ramp pair": two segments of the log, placed immediately back to back with no gap and no overlap, each exactly k minutes long, such that within each of the two segments individually the readings are strictly increasing from one minute to the next (the two segments do not need to relate to each other in value, only each segment must internally be a strict upward ramp).
Formally, find the maximum integer k >= 1 such that there exists a starting index i (using 0-based indexing, with i + 2*k <= n) where nums[i], nums[i+1], ..., nums[i+k-1] is strictly increasing and nums[i+k], nums[i+k+1], ..., nums[i+2*k-1] is also strictly increasing.
A window of length 1 is always trivially a strict ramp (there is nothing to compare), so an answer of at least 1 always exists whenever n >= 2.
Line 1: an integer n, the number of minutes logged.
Line 2: n space-separated integers nums_1 ... nums_n, the speed reading for each minute.
A single integer: the maximum value of k for which a valid ramp pair exists.
2 <= n <= 100.
1 <= nums_i <= 100000.
Example 1
Input
8 5 9 1 4 7 2 6 8
Expected
3
Explanation
With i=2 and k=3, the first segment is nums[2..4] = [1,4,7] (strictly increasing) and the second segment is nums[5..7] = [2,6,8] (strictly increasing), placed back to back with i+2*3=8=n. No k=4 split of this length-8 array can produce two strictly increasing 4-long halves, so the answer is 3.
Example 2
Input
4 3 3 3 3
Expected
1
Explanation
Every reading is equal, so no window of length 2 or more can be strictly increasing (3 is never less than the next 3). Only k=1 (single-element windows) trivially works, 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 →