Stable Trading Windows

Hard
ArraySliding WindowMonotonic QueueContest
You are given n daily prices of an asset and an integer k. A trading window is a contiguous block of days [i, j] with i <= j. A window is stable if, inside it, the highest price minus the lowest price is at most k. Count how many stable windows exist. A single day is always a stable window. The answer can be very large and may exceed a 32-bit integer. Input format: - First line: n k - Second line: n integers (the prices) Print a single integer: the number of stable windows.

Constraints

1 <= n <= 10^5
0 <= k <= 2*10^9
-10^9 <= price[i] <= 10^9

Sample tests

Sample 1

Sample 2

Sign in to submit