text: Minimum and maximum number of digits required to be removed to make a given number divisible by 3 | Function to find the maximum and minimum number of digits to be removed to make str divisible by 3 ; Convert the string leto array of digits ; Count of 0 s , 1 s , and 2 s ; Traverse the array ; Find the sum of digits % 3 ; Cases to find minimum number of digits to be removed ; Cases to find maximum number of digits to be removed ; Driver code ; Function Call
code: function minMaxDigits ( str , N ) { let arr = [ ] ; for ( let i = 0 ; i < N ; i ++ ) arr [ i ] = ( str [ i ] - ' ' ) % 3 ; let zero = 0 , one = 0 , two = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero ++ ; if ( arr [ i ] == 1 ) one ++ ; if ( arr [ i ] == 2 ) two ++ ; } let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum = ( sum + arr [ i ] ) % 3 ; } if ( sum == 0 ) { document . write ( 0 + " " ) ; } if ( sum == 1 ) { if ( ( one != 0 ) && ( N > 1 ) ) document . write ( 1 + " " ) ; else if ( two > 1 && N > 2 ) document . write ( 2 + " " ) ; else document . write ( - 1 + " " ) ; } if ( sum == 2 ) { if ( two != 0 && N > 1 ) document . write ( 1 + " " ) ; else if ( one > 1 && N > 2 ) document . write ( 2 + " " ) ; else document . write ( - 1 + " " ) ; } if ( zero > 0 ) document . write ( N - 1 + " " ) ; else if ( one > 0 && two > 0 ) document . write ( N - 2 + " " ) ; else if ( one > 2 two > 2 ) document . write ( N - 3 + " " ) ; else document . write ( - 1 + " " ) ; } let str = " " ; let N = str . length ; minMaxDigits ( str , N ) ;