Code Logo

Count Increasing Windows

Published at25 Jul 2026
Easy 0 views
Like0

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

Example 1
Input
[1,3,5,2,4,6],3
Output
2
Explanation

[1,3,5] and [2,4,6] are increasing

Example 2
Input
[5,4,3,2,1],3
Output
0
Explanation

All decreasing, none increasing

Example 3
Input
[1,2,3,4],2
Output
3
Explanation

[1,2],[2,3],[3,4] all increasing

Example 4
Input
[1],1
Output
1
Explanation

Single element trivially increasing

Example 5
Input
[],2
Output
0
Explanation

Empty array

Algorithm Flow

Recommendation Algorithm Flow for Count Increasing Windows

Solution Approach

Track increasing adjacent pairs within the sliding window.

function countIncreasing(arr, k)
  if k > length(arr) or length(arr) == 0 then return 0
  count = 0, incPairs = 0
  for i = 1 to k - 1
    if arr[i] > arr[i-1] then incPairs = incPairs + 1
  if incPairs == k - 1 then count = count + 1
  for i = k to length(arr) - 1
    if arr[i-k+1] > arr[i-k] then incPairs = incPairs - 1
    if arr[i] > arr[i-1] then incPairs = incPairs + 1
    if incPairs == k - 1 then count = count + 1
  return count

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

java
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;
    }
}