Number of non | Function to return the required count ; To store the final result ; Two pointer loop ; Initialising j ; Looping till the subarray increases ; Update ret ; Update i ; Return ret ; Driver code
function findCnt ( arr , n , k ) { var ret = 0 ; var i = 0 ; while ( i < n ) { var j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; var x = Math . max ( 0 , j - i - k + 1 ) ; ret += ( x * ( x + 1 ) ) / 2 ; i = j ; } return ret ; } var arr = [ 5 , 4 , 3 , 2 , 1 ] ; var n = arr . length ; var k = 2 ; document . write ( findCnt ( arr , n , k ) ) ;