Count Increasing Windows
Given an array of integers and a window size k, count how many subarrays of length k are strictly increasing (each element is greater than the previous one). Return the count of such windows.
For example, in [1, 2, 3, 4, 5] with k=3, all windows are increasing: [1,2,3], [2,3,4], [3,4,5] — return 3. In [1, 3, 2, 4] with k=3, [1,3,2] is not increasing (2<3), [3,2,4] is not increasing — return 0.
This problem is the mirror of counting decreasing windows. It tracks increasing adjacent pairs using the same sliding window technique with pair counting.
The solution tracks the number of increasing adjacent pairs within the current window. A window of size k is strictly increasing if it has exactly k-1 increasing pairs.
Edge cases include k larger than the array (return 0), k=1 (single-element windows are trivially increasing), and equal adjacent elements (they are not strictly increasing).
Example Input & Output
[1,3,5] and [2,4,6] are increasing
All decreasing, none increasing
[1,2],[2,3],[3,4] all increasing
Single element trivially increasing
Empty array
Algorithm Flow
Solution Approach
Track increasing adjacent pairs within the sliding window.
Count increasing pairs in the first window. Slide: decrement when a pair leaves, increment when a new pair enters. A window is valid when incPairs equals k-1.
Time complexity is O(n), space complexity is O(1).
Best Answers
class Solution {
public int solution(int[] nums, int k) {
if(nums.length<k)return 0;
int v=0;for(int i=1;i<k;i++){if(nums[i]<=nums[i-1])v++;}
int r=v==0?1:0;
for(int i=k;i<nums.length;i++){
if(nums[i-k]>=nums[i-k+1])v--;
if(nums[i]<=nums[i-1])v++;
if(v==0)r++;
}return r;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
