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
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 ( l , r + 1 ) : NEW_LINE INDENT num = i NEW_LINE digits = [ 0 ] * N NEW_LINE for j in range ( N - 1 , - 1 , - 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 ( 1 , N - K + 1 ) : NEW_LINE INDENT curr_sum = 0 NEW_LINE for m in range ( j , j + K ) : NEW_LINE INDENT curr_sum += digits [ m ] NEW_LINE DEDENT if ( sum != curr_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 N = 2 NEW_LINE K = 1 NEW_LINE print ( countDigitSum ( N , K ) ) NEW_LINE