Minimum removals in a number to be divisible by 10 power raised to K | Function to return the required number of digits to be removed ; Converting the given number into string ; variable to store number of digits to be removed ; variable to denote if atleast one zero has been found ; zero found ; return size - 1 if K is not zero and atleast one zero is present , otherwise result ; Driver Code to test above function
function countDigitsToBeRemoved ( N , K ) { var s = N . toString ( ) ; var res = 0 ; var f_zero = 0 ; for ( var i = s . length - 1 ; i >= 0 ; i -- ) { if ( K === 0 ) return res ; if ( s [ i ] === " " ) { f_zero = 1 ; K -- ; } else res ++ ; } if ( K === 0 ) return res ; else if ( f_zero === 1 ) return s . length - 1 ; return - 1 ; } var N = 10904025 ; var K = 2 ; document . write ( countDigitsToBeRemoved ( N , K ) + " " ) ; N = 1000 ; K = 5 ; document . write ( countDigitsToBeRemoved ( N , K ) + " " ) ; N = 23985 ; K = 2 ; document . write ( countDigitsToBeRemoved ( N , K ) + " " ) ;