problems / 161

Steady Stretch

medium sliding-window two-pointers queue arrays 700 ms · 256 MB

Steady Stretch

You are given a sequence of n integers a_1, a_2, ..., a_n and a tolerance K.

A contiguous stretch a_l, a_{l+1}, ..., a_r is called steady if its largest value and its smallest value differ by at most K, that is, max(a_l..a_r) − min(a_l..a_r) ≤ K. Every single element forms a steady stretch on its own.

Find the length of the longest steady stretch. Among all steady stretches of that length, report the one that starts earliest.

Input

The first line contains two integers n and K (1 ≤ n ≤ 10^6, 0 ≤ K ≤ 4·10^9).

The second line contains n integers a_1, a_2, ..., a_n (−2·10^9 ≤ a_i ≤ 2·10^9).

Output

Print two integers: the maximum length L of a steady stretch, and the smallest index l (1-based) such that a_l, ..., a_{l+L−1} is steady.

Notes

In the first example the stretch 4 6 5 7 (positions 3 to 6) has maximum 7 and minimum 4, a difference of 3. No stretch of length 5 is steady, and no other stretch of length 4 is steady.

In the second example there are two steady stretches of length 3, 1 2 2 at positions 1–3 and 5 4 5 at positions 4–6. The earlier one is reported.

Note that the difference between two values can exceed the range of a 32-bit integer.

Example 1

Input

8 3
1 9 4 6 5 7 12 10

Output

4 3

Example 2

Input

6 1
1 2 2 5 4 5

Output

3 1

Hints · reveal one at a time if you're stuck

Hint 1

If a stretch is steady, every stretch inside it is steady too. So for each right end, the valid left ends form a range whose left edge never moves backwards as the right end advances. What does that suggest about scanning the array once?

Hint 2

Use a sliding window. The hard part is knowing the window's maximum and minimum as elements enter on the right and leave on the left, since recomputing them each time is too slow for n = 10^6.

Hint 3

Keep two monotonic deques of indices, one for the maximum and one for the minimum, so both are available in O(1) amortized. Compare max minus min in 64-bit arithmetic, and only update the answer on a strict improvement so the leftmost window is kept.

report an issue Prefer your own editor? sooks prep 161 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.