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
< ? php function countDigitsToBeRemoved ( $ N , $ K ) { $ s = strval ( $ N ) ; $ res = 0 ; $ f_zero = 0 ; for ( $ i = strlen ( $ s ) - 1 ; $ i >= 0 ; $ i -- ) { if ( $ K == 0 ) return $ res ; if ( $ s [ $ i ] == '0' ) { $ f_zero = 1 ; $ K -- ; } else $ res ++ ; } if ( ! $ K ) return $ res ; else if ( $ f_zero ) return strlen ( $ s ) - 1 ; return -1 ; } $ N = 10904025 ; $ K = 2 ; echo countDigitsToBeRemoved ( $ N , $ K ) . " " ; $ N = 1000 ; $ K = 5 ; echo countDigitsToBeRemoved ( $ N , $ K ) . " " ; $ N = 23985 ; $ K = 2 ; echo countDigitsToBeRemoved ( $ N , $ K ) ; ? >