Minimum number of digits required to be removed to make a number divisible by 4 | Function to count the minimum number of digits required to be removed to make a given number divisible by 4 ; Store the size of the string ; Stores the required result ; Check for every pair of digits if the number formed by them is divisible by 4 or not ; Store s [ i ] in a variable ; If it is divisible by 2 ; Store the number formed by s [ j ] and s [ i ] ; Check if it is divisible by 4 ; Store the number of digits required to be deleted ; Update ans ; If value of ans is unchanged , then check if any s [ i ] is divisible by 4 ; If true , update ans to n - 1 ; Prthe result ; Driver Code
def minimumDeletions ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = n NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT t = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( t % 2 == 0 ) : NEW_LINE INDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT num = ( ord ( s [ j ] ) - ord ( '0' ) ) * 10 + t NEW_LINE if ( num % 4 == 0 ) : NEW_LINE INDENT k1 = i - j - 1 NEW_LINE k2 = n - i - 1 NEW_LINE ans = min ( ans , k1 + k2 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( ans == n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT num = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( num % 4 == 0 ) : NEW_LINE INDENT ans = n - 1 NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "12367" NEW_LINE minimumDeletions ( str ) NEW_LINE DEDENT