Reduce the number to minimum multiple of 4 after removing the digits | Python3 implementation of the approach ; Function to return the minimum number that can be formed after removing the digits which is a multiple of 4 ; For every digit of the number ; Check if the current digit is divisible by 4 ; If any subsequence of two digits is divisible by 4 ; Driver code
import sys NEW_LINE TEN = 10 NEW_LINE def minNum ( str , len1 ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT if ( str [ i ] == '4' or str [ i ] == '8' ) : NEW_LINE INDENT res = min ( res , ord ( str [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT DEDENT for i in range ( len1 - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , len1 , 1 ) : NEW_LINE INDENT num = ( ord ( str [ i ] ) - ord ( '0' ) ) * TEN + ( ord ( str [ j ] ) - ord ( '0' ) ) NEW_LINE if ( num % 4 == 0 ) : NEW_LINE INDENT res = min ( res , num ) NEW_LINE DEDENT DEDENT DEDENT if ( res == sys . maxsize ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return res NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "17" NEW_LINE len1 = len ( str ) NEW_LINE print ( minNum ( str , len1 ) ) NEW_LINE DEDENT