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 ; Driver Code ; Given integer N and K
def countDigitSum ( N , K ) : NEW_LINE INDENT l = pow ( 10 , N - 1 ) ; NEW_LINE r = pow ( 10 , N ) - 1 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( 1 , r + 1 ) : NEW_LINE INDENT num = i ; NEW_LINE digits = [ 0 ] * ( N ) ; NEW_LINE for j in range ( N - 1 , 0 , - 1 ) : NEW_LINE INDENT digits [ j ] = num % 10 ; NEW_LINE num //= 10 ; NEW_LINE DEDENT sum = 0 ; NEW_LINE flag = 0 ; NEW_LINE for j in range ( 0 , K ) : NEW_LINE INDENT sum += digits [ j ] ; NEW_LINE DEDENT for j in range ( K , N ) : NEW_LINE INDENT if ( sum - digits [ j - K ] + digits [ j ] != sum ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 ; NEW_LINE K = 1 ; NEW_LINE print ( countDigitSum ( N , K ) ) ; NEW_LINE DEDENT