Count of N digit Numbers whose sum of every K consecutive digits is equal | Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits using sliding window ; Given integer N and K
function countDigitSum ( N , K ) { var l = parseInt ( Math . pow ( 10 , N - 1 ) ) , var r = parseInt ( Math . pow ( 10 , N ) - 1 ) ; var count = 0 ; for ( i = l ; i <= r ; i ++ ) { var num = i ; var digits = Array ( N ) . fill ( 0 ) ; for ( j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num = parseInt ( num / 10 ) ; } var sum = 0 , flag = 0 ; for ( j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( j = K ; j < N ; j ++ ) { if ( sum - digits [ j - K ] + digits [ j ] != sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } var N = 2 , K = 1 ; document . write ( countDigitSum ( N , K ) ) ;