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 ; Print the result ; Driver Code
function minimumDeletions ( s ) { let n = s . length ; let ans = n ; for ( let i = n - 1 ; i >= 0 ; i -- ) { let t = s [ i ] - ' ' ; if ( t % 2 == 0 ) { for ( let j = i - 1 ; j >= 0 ; j -- ) { let num = ( s [ j ] - ' ' ) * 10 + t ; if ( num % 4 === 0 ) { let k1 = i - j - 1 ; let k2 = n - i - 1 ; ans = Math . min ( ans , k1 + k2 ) ; } } } } if ( ans === n ) { for ( let i = 0 ; i < n ; i ++ ) { let num = s [ i ] - ' ' ; if ( num % 4 === 0 ) { ans = n - 1 ; } } } document . write ( ans ) ; } let str = " " ; minimumDeletions ( str ) ;