Count maximum concatenation of pairs from given array that are divisible by 3 | Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Check if an element present in any pair or not ; Generate all possible pairs ; If the element already present in a pair ; If the element already present in a pair ; If concatenation of elements is divisible by 3 ; Update ans ; Mark i is True ; Mark j is True ; Driver Code ; To display the result
function countDivBy3InArray ( arr ) { let ans = 0 ; let taken = new Array ( arr . length ) ; taken . fill ( false ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( taken [ i ] == true ) { continue ; } for ( let j = i + 1 ; j < arr . length ; j ++ ) { if ( taken [ j ] == true ) { continue ; } if ( parseInt ( ( arr [ i ] ) . toString ( ) + ( arr [ j ] ) . toString ( ) , 10 ) % 3 == 0 || parseInt ( ( arr [ j ] ) . toString ( ) + ( arr [ i ] ) . toString ( ) , 10 ) % 3 == 0 ) { ans += 1 ; taken [ i ] = true ; taken [ j ] = true ; } } } return ans ; } let arr = [ 5 , 3 , 2 , 8 , 7 ] ; document . write ( countDivBy3InArray ( arr ) ) ;