left
and right
to 0, which will represent the bounds of the sliding window.windowSum
to 0, which will keep track of the sum of elements within the window.valid
to 0, which will count the number of valid subarrays.right
pointer over the array, adding the current element to windowSum
.windowSum * (right - left + 1)
) is greater than or equal to k
, shrink the window from the left by subtracting the left
element from windowSum
and incrementing left
.right
, add the number of valid subarrays ending at right
to valid
, which is right - left
.right
has reached the end of the array.valid
.