Number of ways to form a number with maximum Ks in it | Function to calculate number of ways a number can be formed that has the maximum number of Ks ; Convert to string ; Calculate length of subarrays that can contribute to the answer ; Count length of subarray where adjacent digits add up to K ; Current subarray can contribute to the answer only if it is odd ; Return the answer ; Driver code ; Input ; Function call
def noOfWays ( N , K ) : NEW_LINE INDENT S = str ( N ) NEW_LINE ans = 1 NEW_LINE for i in range ( 1 , len ( S ) ) : NEW_LINE INDENT count = 1 NEW_LINE while ( i < len ( S ) and ord ( S [ i ] ) + ord ( S [ i - 1 ] ) - 2 * ord ( '0' ) == K ) : NEW_LINE INDENT count += 1 NEW_LINE i += 1 NEW_LINE DEDENT if ( count % 2 ) : NEW_LINE INDENT ans *= ( count + 1 ) // 2 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 1454781 NEW_LINE K = 9 NEW_LINE print ( noOfWays ( N , K ) ) NEW_LINE DEDENT