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 ; Input ; Function call
function noOfWays ( N , K ) { let S = N . toString ( ) ; let ans = 1 ; for ( let i = 1 ; i < S . length ; i ++ ) { let count = 1 ; while ( i < S . length && S [ i ] . charCodeAt ( ) - 48 + S [ i - 1 ] . charCodeAt ( ) - 48 == K ) { count ++ ; i ++ ; } if ( count % 2 == 1 ) ans *= Math . floor ( ( count + 1 ) / 2 ) ; } return ans ; } let N = 1454781 ; let K = 9 ; document . write ( noOfWays ( N , K ) ) ;