text: Minimum removals in a number to be divisible by 10 power raised to K | Java Program to count the number of digits that can be removed such that number is divisible by 10 ^ 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
code: public class GFG { static int countDigitsToBeRemoved ( int N , int K ) { String s = Integer . toString ( N ) ; int res = 0 ; int f_zero = 0 ; for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { if ( K == 0 ) return res ; if ( s . charAt ( i ) == '0' ) { f_zero = 1 ; K -- ; } else res ++ ; } if ( K == 0 ) return res ; else if ( f_zero == 1 ) return s . length ( ) - 1 ; return - 1 ; } public static void main ( String [ ] args ) { int N = 10904025 ; int K = 2 ; System . out . println ( countDigitsToBeRemoved ( N , K ) ) ; N = 1000 ; K = 5 ; System . out . println ( countDigitsToBeRemoved ( N , K ) ) ; N = 23985 ; K = 2 ; System . out . println ( countDigitsToBeRemoved ( N , K ) ) ; } }