Find the maximum number of elements divisible by 3 | Function to find the maximum number of elements divisible by 3 ; To store frequency of each number ; Store modulo value ; Store frequency ; Add numbers with zero modulo to answer ; Find minimum of elements with modulo frequency one and zero ; Add k to the answer ; Remove them from frequency ; Add numbers possible with remaining frequency ; Return the required answer ; Driver code ; Function call
function MaxNumbers ( a , n ) { let fre = new Array ( 3 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { a [ i ] %= 3 ; fre [ a [ i ] ] ++ ; } let ans = fre [ 0 ] ; let k = Math . min ( fre [ 1 ] , fre [ 2 ] ) ; ans += k ; fre [ 1 ] -= k ; fre [ 2 ] -= k ; ans += parseInt ( fre [ 1 ] / 3 ) + parseInt ( fre [ 2 ] / 3 ) ; return ans ; } let a = [ 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 ] ; let n = a . length ; document . write ( MaxNumbers ( a , n ) ) ;