KatInterview

Count Strictly Rising Signal Windows

Arrays & HashingAuto-graded · 21 test cases
EasyAuto-gradedArrays & Hashingarrayssliding-windowcounting

Last updated

Count how many contiguous subarrays of length exactly k have strictly increasing values in a signal array.

You are monitoring a signal over n time steps. You are given:

  • An integer n, the total number of readings.
  • An integer k (1 ≤ k ≤ n), the required window length.
  • An integer array signal of length n, where signal[i] is the signal strength at time step i (0-based).

A window signal[l..r] (0 ≤ l ≤ r < n) is strictly rising if for every index i with l ≤ i < r, the value signal[i] < signal[i + 1].

Count the number of windows of length exactly k that are strictly rising. Formally, count the number of starting indices s such that 0 ≤ s ≤ n − k and signal[s] < signal[s + 1] < … < signal[s + k − 1].

Return a single integer: the count of strictly rising windows of length k.

Constraints:

  • 1 ≤ k ≤ n
  • 1 ≤ n ≤ 2 × 10⁵
  • Each element of signal is an integer (may be negative, zero, or positive).
  • Equal adjacent values break a strictly rising run.
  • When k = 1, every single-element window is trivially strictly rising, so the answer is n.

Examples

Example 1

Input: count_rising_windows(5, 3, [1, 2, 3, 4, 5])

Output: 3

Example 2

Input: count_rising_windows(4, 2, [1, 2, 1, 2])

Output: 2

Example 3

Input: count_rising_windows(4, 1, [4, 3, 2, 1])

Output: 4

Hints

Hint 1

Instead of checking every window from scratch, track the length of the current strictly increasing streak as you scan left to right.

Hint 2

When k is 1, every single element qualifies on its own, so handle that as a special case returning n.

Hint 3

A length-k window ending at index i is valid exactly when the increasing streak ending at i has length at least k, so increment your count each time the streak reaches k.