Number of digits to be removed to make a number divisible by 3 | function to count the no of removal of digits to make a very large number divisible by 3 ; add up all the digits of num ; if num is already is divisible by 3 then no digits are to be removed ; if there is single digit , then it is not possible to remove one digit . ; traverse through the number and find out if any number on removal makes the sum divisible by 3 ; if there are two numbers then it is not possible to remove two digits . ; Otherwise we can always make a number multiple of 2 by removing 2 digits . ; Driver Code
function divisible ( num ) { let n = num . length ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += ( num . charAt ( i ) ) ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( let i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num . charAt ( i ) - ' ' ) % 3 ) return 1 ; if ( n == 2 ) return - 1 ; return 2 ; } let num = " " ; document . write ( divisible ( num ) ) ;