Count Increasing Windows
Given an integer array nums and a window size k, count how many contiguous subarrays of length k are strictly increasing. A window is strictly increasing if each element is greater than the previous element.
Check the first k elements. Then slide the window one position at a time. When an element leaves the window and a new element enters, only the pair (nums[i-1], nums[i]) for the entering element needs to be checked to determine if the window remains strictly increasing. Track the count of violated pairs within the current window. When the violation count is zero, the window is valid.
Time is O(n) with O(1) space. Each element is compared with its predecessor at most twice: once when it enters the window and once when it leaves.
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
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.
