Number of non | C ++ implementation of the approach ; Function to return the count of increasing subarrays of length k ; To store the final result ; Two pointer loop ; Initialising j ; Looping till the subarray increases ; Updating the required count ; Updating i ; Returning res ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntSubArrays ( int * arr , int n , int k ) { int res = 0 ; int i = 0 ; while ( i < n ) { int j = i + 1 ; while ( j < n and arr [ j ] > = arr [ j - 1 ] ) j ++ ; res += max ( j - i - k + 1 , 0 ) ; i = j ; } return res ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 2 , 5 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 2 ; cout << cntSubArrays ( arr , n , k ) ; return 0 ; }