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 ; If sum is not equal then break the loop ; Increment the count if it satisfy the given condition ; Given N and K ; Function call
function countDigitSum ( N , K ) { let l = parseInt ( Math . pow ( 10 , N - 1 ) ) , r = parseInt ( Math . pow ( 10 , N ) - 1 ) ; let count = 0 ; for ( let i = l ; i <= r ; i ++ ) { let num = i ; let digits = new Array ( N ) ; for ( let j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num = parseInt ( num / 10 ) ; } let sum = 0 , flag = 0 ; for ( let j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( let j = 1 ; j < N - K + 1 ; j ++ ) { let curr_sum = 0 ; for ( let m = j ; m < j + K ; m ++ ) curr_sum += digits [ m ] ; if ( sum != curr_sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } let N = 2 , K = 1 ; document . write ( countDigitSum ( N , K ) ) ;